1

我有一个正在开发的 Web 应用程序,它使用声明身份验证,但是每当有人刷新页面时,我遇到了一个问题,它会丢弃登录的用户。这听起来与这里的问题非常相似:https ://www.sitefinity.com/developer-network/forums/developing-with-sitefinity-/httpcontext-current-user-identity-isauthenticated-is-false-after-response-重定向但不幸的是我不能只切换到基于表单的身份验证,因为我还需要使用 openid 东西连接到 facebook 等。

我的 global.asax.cs 中有以下代码

protected void Application_Start(object sender, EventArgs e)
        {
            Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += Bootstrapper_Initialized;
            //  RegisterWebApiFilters(GlobalConfiguration.Configuration.Filters);
            System.Web.Helpers.AntiForgeryConfig.UniqueClaimTypeIdentifier = System.Security.Claims.ClaimTypes.NameIdentifier;            
        }

 protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            var identity = System.Web.HttpContext.Current.User.Identity as System.Security.Claims.ClaimsIdentity;
            var claimsUser = ClaimsManager.GetCurrentIdentity();
            //identity.AddClaim(new System.Security.Claims.Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", identity.Name));
            identity.AddClaim(new System.Security.Claims.Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", identity.Name));
            //System.Web.Helpers.AntiForgeryConfig.UniqueClaimTypeIdentifier = System.Security.Claims.ClaimTypes.NameIdentifier;
        }

上面代码中的标识变量在重新加载页面之前显示登录用户正常,包括它存储在 cookie 中,但在重新加载时以匿名用户身份返回。

我想我也正确配置了 web.config。我将身份模型设置为以下内容:

<system.identityModel.services>
    <federationConfiguration>
      <wsFederation passiveRedirectEnabled="true" issuer="http://localhost" realm="http://localhost" requireHttps="false" />
      <cookieHandler requireSsl="false" />
    </federationConfiguration>

任何想法,将不胜感激。

4

1 回答 1

0

我认为您在提交任何表单数据并将代码放在 Application_AuthenticateRequest 事件中时遇到了 AntiforgeryToken 错误,试试下面的代码可能对您有用!因为对于每个请求,您都在添加声明...当我检查每个请求的声明时,如果未添加,则添加声明,否则逾越节。

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        var identity = System.Web.HttpContext.Current.User.Identity as System.Security.Claims.ClaimsIdentity;

        if (!identity.FindAll(System.Security.Claims.ClaimTypes.NameIdentifier).Any())
        {
            identity.AddClaim(new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.NameIdentifier, System.Web.HttpContext.Current.User.Identity.Name));
        }
        System.Web.Helpers.AntiForgeryConfig.UniqueClaimTypeIdentifier = System.Security.Claims.ClaimTypes.NameIdentifier;
    }
于 2018-04-11T10:49:29.717 回答