Asp.net mvc 从未经授权的页面重定向用户

Asp.net mvc 从未经授权的页面重定向用户,asp.net-mvc,authorization,forms-authentication,federated-identity,Asp.net Mvc,Authorization,Forms Authentication,Federated Identity,我有一个MVC应用程序,我最近将身份验证/授权方法从表单转换为联邦。一切正常,但在主页上,我必须创建一个cookie,以授权我的站点的其余部分正常工作。当用户首先导航到主页时,效果很好,如果他们首先导航到另一个需要授权的页面,他们会得到一个错误页面 当我实现表单身份验证时,它会将未经授权的用户重定向到登录页面,使用federation,我不再有登录页面,因此我希望重定向到主页。使用表单身份验证时,重定向是自动的,如何为我的联邦应用程序设置类似的内容 下面是我的web.config的相关联邦部分。

我有一个MVC应用程序,我最近将身份验证/授权方法从表单转换为联邦。一切正常,但在主页上,我必须创建一个cookie,以授权我的站点的其余部分正常工作。当用户首先导航到主页时,效果很好,如果他们首先导航到另一个需要授权的页面,他们会得到一个错误页面

当我实现表单身份验证时,它会将未经授权的用户重定向到登录页面,使用federation,我不再有登录页面,因此我希望重定向到主页。使用表单身份验证时,重定向是自动的,如何为我的联邦应用程序设置类似的内容

下面是我的web.config的相关联邦部分。同样,联合身份验证/授权是有效的,只是未经授权的重定向无效

  <system.web>
    <customErrors mode="Off"/>
    <authentication mode="None"/>
    <authorization>
      <deny users="?"/>
    </authorization>

    <membership defaultProvider="ADMembershipProvider">
      <providers>
        <add name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider" connectionProtection="Secure" attributeMapUsername="sAMAccountName" connectionStringName="ADConn" connectionUsername="UName" connectionPassword="Pass" />
      </providers>
    </membership>
    <roleManager enabled="true" defaultProvider="ActiveDirectoryRoleProvider" cacheRolesInCookie="true" cookieName=".ADLibraryROLES" cookiePath="/" cookieTimeout="1440" cookieRequireSSL="false" cookieSlidingExpiration="true" createPersistentCookie="true" cookieProtection="All">
      <providers>
        <clear />
        <add name="ActiveDirectoryRoleProvider" connectionStringName="ADConn" connectionUsername="UName" connectionPassword="Pass" attributeMapUsername="sAMAccountName" type="MyApp.ActiveDirectoryRoleProvider" />
      </providers>
    </roleManager>
  </system.web>
  <system.webServer>
      <modules>
        <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>
    <identityConfiguration>
      <audienceUris>
        <add value="https://fed.example.com/"/>
      </audienceUris>
      <securityTokenHandlers>
        <add type="System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
        <remove type="System.IdentityModel.Tokens.SessionSecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      </securityTokenHandlers>
      <certificateValidation certificateValidationMode="None"/>
      <issuerNameRegistry type="System.IdentityModel.Tokens.ValidatingIssuerNameRegistry, System.IdentityModel.Tokens.ValidatingIssuerNameRegistry">
        <authority name="http://myfedservice.example.com/adfs/services/trust">
          <keys>
            <add thumbprint="mythumb"/>
          </keys>
          <validIssuers>
            <add name="http://fed.example.com/adfs/services/trust"/>
          </validIssuers>
        </authority>
      </issuerNameRegistry>
    </identityConfiguration>
  </system.identityModel>
  <system.identityModel.services>
    <federationConfiguration>
      <cookieHandler requireSsl="true"/>
      <wsFederation passiveRedirectEnabled="true" issuer="https://fed.example.com/adfs/ls/" realm="https://fed.example.com/" reply="https://fed.example.com/" requireHttps="true" persistentCookiesOnPassiveRedirects="true"/>
    </federationConfiguration>
  </system.identityModel.services>

您可以在
wsFederation
部分中配置此功能,有关详细信息,请参阅。通过将“被动式DirectEnabled”设置为true,WSFederationAuthenticationModule将查看所有传出响应,尝试查找HTTP 401。如果它找到一个401,它将修改响应并将其转换为指向STS的重定向。请注意,在生产中,您希望将
requireHttps
更改为
true

<system.identityModel.services>
<federationConfiguration>
  <wsFederation passiveRedirectEnabled="true" 
    issuer="http://localhost:15839/wsFederationSTS/Issue" 
    realm="http://localhost:50969/" reply="http://localhost:50969/" 
    requireHttps="false" 
    signOutReply="http://localhost:50969/SignedOutPage.html" 
    signOutQueryString="Param1=value2&amp;Param2=value2" 
    persistentCookiesOnPassiveRedirects="true" />
  <cookieHandler requireSsl="false" />
</federationConfiguration>

请注意,您还需要添加以下模块:

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

以及以下配置部分:

<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />


除了signOutReply和signOutQueryString之外,我什么都有,但它不起作用。这些是必需的吗?我不知道我会在查询字符串中输入什么,而且我目前没有注销页面。您可以忽略这些,它们不是必需的。请查看更多详细信息。我似乎也有你链接的博客中的所有建议,但仍然没有重定向。还有其他建议吗?您在IIS中启用了什么身份验证模式?您应该已启用匿名身份验证。正确,仅启用匿名身份验证。我想如果没有它,联邦就根本无法运作。联合身份验证工作得很好,只是当用户尝试访问他们无权查看的页面时,不会重定向。我尝试创建自定义重定向,检查授权(似乎正在工作),如果未授权,则重定向到创建联合cookie的主页。问题是当用户未经授权而不是像我预期的那样重定向回家时,我会出现“网站有重定向循环”错误。为什么会发生这种情况??