從 ASP.NET MVC 4 項目中的 WIF 授權中排除特定路徑
**在 Visual Studio 2012 的Identity and Access…**擴展的幫助下,我們已經在 ASP.NET 4.5 MVC 4 項目中成功配置了 Windows Identity Foundation (WIF) 。但無法從授權中排除特定路徑以允許匿名訪問.
當我們訪問我們的預設路由(即
/Home)時,被動重定向會將我們重定向到配置的頒發者 Uri。這是正確的。但是現在假設我們要從 STS 身份驗證中排除路徑/Guest,以便每個人都可以訪問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>此外,我們有…
<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 回答,因為這是更有用的回答。
[Authorize]在 MVC 應用程序中,您通常通過控制器和操作中的屬性定義訪問權限。只需從 web.config 中刪除:
<system.web> <authorization> <deny users="?" /> </authorization>注意:這通常是由VS2010 中的*“添加 STS 引用”嚮導自動添加的*
似乎在 VS2012 和新工具上的行為完全相同。我剛剛創建了一個全新的 MVC4 應用程序。使用本地配置 STS 執行“身份和訪問…”工具(保留所有預設值)。
它確實將此片段添加到 web.config:
<authorization> <deny users="?" /> </authorization>我將其刪除並添加
[Authorize]到 About 控制器操作中:[Authorize] public ActionResult About() { ViewBag.Message = "Your app description page."; return View(); }當我點擊“關於”連結時,我會被重定向到 STS。其他一切都適用於匿名訪問。
筆記:
您也可以在嚮導中對此進行一些控制(參見嚮導的“配置”頁面)。