2012-11-08 21 views
11

我们已经在我们的ASP.NET 4.5 MVC 4项目中借助标识和访问成功配置了windows身份基础(WIF)...扩展对于Visual Studio 2012.但无法从授权中排除特定路径以允许匿名访问。在ASP.NET MVC 4项目中排除WIF授权的特定路径

当我们进入我们的默认路由(即/Home),被动重定向将我们重定向到配置的发布者URI。这是正确的。但是,现在假设我们想从STS认证排除路径/Guest,让大家可以在不beeing路由到STS发行人访问http://ourhost/Guest。只有静态文档位于那里。从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> 

而且我们有...

<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> 

最后:

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

我们尝试没有成功如下:

<location path="~/Guest"> <!-- also "/Guest" is not working --> 
    <system.web> 
    <authorization> 
     <allow users="*" /> 
    </authorization> 
    </system.web> 
</location> 

我们也试图把一个小的Web.config文件放到这个文件夹,但没有成功。无论我们在浏览器中找到哪个Uri,我们都会重定向。

什么是实现这一目标的正确方法?

编辑

删除了以前的“接受回答”,将“接受答案” Eugenios answer,因为这是比较有用的回复。

回答

11

在一个MVC应用程序通常会定义通过控制器和行动[Authorize]属性的访问。

刚刚从web.config中删除:

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

注:这通常是由自动添加在VS2010

“添加STS引用”向导

看来这个行为是完全一样的VS2012和新工具。我刚创建了一个全新的MVC4应用程序。使用本地配置STS运行“身份和访问...”工具(保留所有缺省值)。

它没有这个片段添加到Web.config:

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

我删除,并添加[Authorize]到关于控制器动作:

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

    return View(); 
} 

当我点击“关于”链接,然后我重定向到STS。其他的一切都与匿名访问一起工作。

注:

你有太多的向导在此方面的控制(见向导的“配置”页)。

+0

谢谢欧金尼奥,我们使用Visual Studio 2012和 “添加STS引用” 不在那里可用,你必须使用“身份和交流cess ...“工具。我认为我接近解决方案,看起来像'passiveRedirectEnabled =“true”'将我重定向到STS,而不管任何属性的用法。设置为“false”并在自定义身份验证属性中使用一些重定向助手重定向似乎相当不错。完成后会发布我的解决方案,谢谢! – thmshd

+0

我明白了。它也应该与passiveRedirectEnabled = true一起工作。我也会检查我的系统。 –

+0

因为我确实遇到了启用passiveRedirectEnabled选项的问题,所以我想接受我自己的答案作为解决方案,但是我真的很感谢你对这个主题的帮助,我相信它也可能工作,但也许解决方案是埋在某个地方我的'web.config'设置结合IIS设置。谢谢你的时间,真的很感激! – thmshd

2

终于指出我正确的方向是一个更老的blog post它解释了如何保护一个特定的控制器或页面区域。结合全球过滤器,我几乎就在那里。

似乎关键不是使用passiveRedirectEnabled="true"选项,而是将其设置为false。只有这样你才能完全控制身份验证过程,但是需要使用SignInRequestMessage类(这不是什么大问题)自己触发被动重定向。

欢迎使用较少代码的更好解决方案。

编辑

删除“接受回答”状态这一点,将“接受的答案” Eugenios前面回答,因为这是比较有用的回复。

2

我和托马斯处于同样的境地。就我而言,我在本地测试/使用IISExpress。

欧金尼奥的回答几乎让我工作,有一个额外的要求。我必须在我的MVC项目属性中将“匿名身份验证”设置为“已启用”。

这是默认禁用的,或者可能在使用VS 2012“Identity and Access ...”工具时设置的。

因此,回顾一下,没有代码或特殊的属性来编写/维护。

我的csproj文件包含:

<IISExpressAnonymousAuthentication>enabled</IISExpressAnonymousAuthentication> 

我的web.config包含:

<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> 

而且,我添加标准[授权]属性控制器,我想通过WIF辩护行动:

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

我不能得到[Authorize]工作 - 它没有做重定向我的STS,我相信这是我失踪的东西。不过,我确实发现了如何解决原来的问题。

global.asax

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

然后:

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

谢谢Andrew,不过我想指出,这种方法会产生一个安全漏洞,因为我现在可以访问任何我想要的控制器和动作,因为我可以在URL的末尾添加如下内容:“#/ Content /“,并且不会改变初始路由,但它会让我访问控制器。 –

+0

这并不适用于我,而接受的答案没有任何其他更改。我仍然得到了401的回应。 – itslittlejohn