从ASP.NET MVC 4项目中排除WIF授权的特定路径

thm*_*shd 11 asp.net-mvc claims-based-identity wif asp.net-mvc-4

我们已经在Visual Studio 2012 的Identity and Access ...扩展的帮助下,在ASP.NET 4.5 MVC 4项目中成功配置了Windows身份基础(WIF).但是无法从授权中排除特定路径以允许匿名访问.

当我们访问我们的默认路由(即/Home)时,被动重定向会将我们重定向到配置的发行者Uri.这是当前的.但现在假设我们想要/Guest从STS身份验证中排除Path ,这样每个人都可以访问http://ourhost/Guest而无需路由到STS颁发者.只有静态文档位于那里.

片段来自Web.config:

<system.identityModel>
  <identityConfiguration>
    <audienceUris>
      <add value="http://ourhost/" />
    </audienceUris>
    <issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <trustedIssuers>
        <add thumbprint="9B74****40D0" name="OurSTS" />
      </trustedIssuers>
    </issuerNameRegistry>
    <certificateValidation certificateValidationMode="None" />
  </identityConfiguration>
</system.identityModel>
<system.identityModel.services>
  <federationConfiguration>
    <cookieHandler requireSsl="false" />
    <wsFederation passiveRedirectEnabled="true" issuer="http://oursts/Issue" realm="http://ourhost/" reply="http://ourhost/" requireHttps="false" />
  </federationConfiguration>
</system.identityModel.services>
Run Code Online (Sandbox Code Playgroud)

我们还有......

<system.webServer>
  <!-- ... -->
  <modules runAllManagedModulesForAllRequests="true">
    <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" />
    <remove name="FormsAuthentication" />
  </modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

最后:

<system.web>
  <!-- ... -->
  <authentication mode="None" />
</system.web>
Run Code Online (Sandbox Code Playgroud)

我们尝试了以下但没有成功:

<location path="~/Guest"> <!-- also "/Guest" is not working -->
  <system.web>
    <authorization>
      <allow users="*" />
    </authorization>
  </system.web>
</location>
Run Code Online (Sandbox Code Playgroud)

我们还尝试将一个小的Web.config文件放入此文件夹,但没有成功.无论我们在浏览器中找到哪个Uri,我们总是被重定向.

完成此任务的正确方法是什么?

编辑

删除了之前的"接受的答案",将"接受的答案"设置为Eugenios答案,因为这是更有用的答复.

Eug*_*ace 12

在MVC应用程序中,您通常通过[Authorize]控制器和操作中的属性定义访问权限.

只需从web.config中删除:

<system.web>
     <authorization>
        <deny users="?" />
      </authorization>
Run Code Online (Sandbox Code Playgroud)

注意:这通常由VS2010中的"添加STS参考"向导自动添加

似乎VS2012和新工具的行为完全相同.我刚刚创建了一个全新的MVC4应用程序.使用本地配置STS(保留所有默认值)运行"身份和访问..."工具.

它确实将此片段添加到web.config:

<authorization>
  <deny users="?" />
</authorization>
Run Code Online (Sandbox Code Playgroud)

我删除了它并添加[Authorize]到About控制器操作:

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

    return View();
}
Run Code Online (Sandbox Code Playgroud)

当我点击"关于"链接时,我会被重定向到STS.其他一切都与匿名访问一起使用.

注意:

您也可以在向导中对此进行控制(请参阅向导的"配置"页面).