【问题标题】:Sitefinity losing user session on reload with claims authenticationSitefinity 在使用声明身份验证重新加载时失去用户会话
【发布时间】:2018-04-04 15:36:08
【问题描述】:

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

任何想法都将不胜感激。

【问题讨论】:

    标签: sitefinity sitefinity-10


    【解决方案1】:

    我认为您在提交任何表单数据并将代码放在 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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-15
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      相关资源
      最近更新 更多