Exclude specific path from WIF authorization in a ASP.NET MVC 4 project

江枫思渺然 提交于 2019-12-03 11:36:38

In an MVC app you typically define access through the [Authorize] attribute in controllers and actions.

Just remove from web.config:

<system.web>
     <authorization>
        <deny users="?" />
      </authorization>

Note: this is usually added automatically by the "Add STS Reference" wizard in VS2010

It seems that the behaviour is exactly the same on VS2012 and the new tools. I just created a brand new MVC4 app. Ran the "Identity and Access..." tool with a local config STS (left all defaults).

It did add this fragment to the web.config:

<authorization>
  <deny users="?" />
</authorization>

I removed it and added [Authorize] to the About controller action:

[Authorize]
public ActionResult About()
{
    ViewBag.Message = "Your app description page.";

    return View();
}

When I click on the "About" link, then I get redirected to the STS. Everything else works with anonymous access.

Note:

You have some control on this too in the wizard (see the "Configuration" page of the wizard).

I can not get [Authorize] to work - it is not doing the redirect to my STS, and I am sure it is something I am missing. I did discover how to solve for the original ask, though.

In global.asax:

    protected void Application_Start()
    {
        ... config stuff ...
        FederatedAuthentication.WSFederationAuthenticationModule.AuthorizationFailed += WSFederationAuthenticationModule_AuthorizationFailed;
    }

and then:

    void WSFederationAuthenticationModule_AuthorizationFailed(object sender, AuthorizationFailedEventArgs e)
    {
        // Do path/file detection here
        if (Request.Path.Contains("/Content/") || Request.Path.Contains("/Scripts/"))
        {
            e.RedirectToIdentityProvider = false;
        }
    }

I was in the same situation as Thomas. In my case, I was testing/using IISExpress locally.

Eugenio's answer almost got me working, with one added requirement. I had to set the "Anonymous Authentication" in my MVC Project Property to "Enabled."

This was either disabled by default or possibly set that way when using the VS 2012 "Identity and Access..." tooling.

So, to recap, there was no code or special attributes to write/maintain.

My csproj file contains:

<IISExpressAnonymousAuthentication>enabled</IISExpressAnonymousAuthentication>

My web.config contains:

<system.web>
    <authentication mode="None" />
</system.web>

<system.web>
    <authorization>
        <allow users="*" />
    </authorization>
</system.web>

<system.webServer>
    <modules>
        <remove name="FormsAuthentication" />
        <add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
        <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
    </modules>
</system.webServer>

<system.identityModel.services>
    <federationConfiguration>
        <wsFederation passiveRedirectEnabled="true" issuer="https://REMOVED.accesscontrol.windows.net/v2/wsfederation" realm="urn:REMOVED" requireHttps="false" />
    </federationConfiguration>
</system.identityModel.services>

And, I add the standard [Authorize] attribute to controller actions that I want to be defended by WIF:

[Authorize]
public ActionResult About()
{
....
}

What finally pointed me into the right direction was an older blog post which explains how to protect a specific controller or area of the page. In combination with global filters I'm almost there.

It seems like the key is not to use the passiveRedirectEnabled="true" option but set it to false. Only then you have the full control over the authentication process, but would need to trigger the passive redirection yourself then, using the SignInRequestMessage class (which is not a big deal).

Better solutions with less code required are welcome.

EDIT

Removed "accepted answered" state for this, set "accepted answer" to Eugenios anwer as this is the more useful reply.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!