不存在用户身份声明

问题描述 投票:0回答:1

我正在尝试将令牌添加到我的用户声明中,但该令牌不会持续存在。通过UserManager

添加其他所有声明都可以

注意AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie

我也尝试过(User.Identity as ClaimsIdentity)?.AddClaim

[HttpContext.Current.GetOwinContext().Authentication.SignOut()也在调用错误的端点

          app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                CookieSecure =  CookieSecureOption.SameAsRequest,
                CookieName = ApplicationCookieName,
             }
            });

            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);


            ConfigureIdentityServer(app);
        }
           app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                AuthenticationType = "IdentityServer",

                RequireHttpsMetadata = false, // TODO DEV

                Authority = authority,

                ClientId = clientId,
                ClientSecret = clientSecret,

                RedirectUri = redirectUri,
                PostLogoutRedirectUri = postLogoutRedirectUri,

                ResponseType = OpenIdConnectResponseType.CodeIdTokenToken,
                Scope = scope
          }


        public async Task<IHttpActionResult> Callback()
        {
            var returnUrl = DefaultReturnUrl;
            var errorUrl = DefaultErrorUrl;
            var unauthorizedUrl = DefaultUnauthorizedUrl;

            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

            var signInStatus = await SignIngManager.ExternalSignInAsync(loginInfo, true);            

            if (signInStatus != SignInStatus.Success)
            {             
                return new RedirectActionResult($"{unauthorizedUrl}?error={signInStatus:G}");
            }


            // keep the id_token for logout
            (AuthenticationManager.User.Identity as ClaimsIdentity)?.AddClaim(new Claim(TokenClaimTypes.IdToken, loginInfo.ExternalIdentity.Claims
                .Where(c => c.Type == TokenClaimTypes.IdToken).Select(c => c.Value)
                .SingleOrDefault()));


            return new RedirectActionResult($"{returnUrl}#SignIn")
            {
                CookieHeaderValues =
                    CookieAuthorizationHelper.GetAuthorizedCookieHeaderValues(AuthenticationManager
                        .AuthenticationResponseGrant.Identity)
            };
        }

显然已添加,但是当我从另一个电话接连检查时,用户声称没有令牌

s

c# asp.net asp.net-mvc asp.net-identity owin
1个回答
0
投票

您可以创建一个新的ClaimsIdentity,然后使用此类来更新声明。

// get context of the authentication manager
var authenticationManager = HttpContext.GetOwinContext().Authentication;

// create a new identity from the old one
var identity = new ClaimsIdentity(User.Identity);

// update claim value
identity.RemoveClaim(identity.FindFirst("AccountNo"));
identity.AddClaim(new Claim("AccountNo", value));

// tell the authentication manager to use this new identity
authenticationManager.AuthenticationResponseGrant = 
    new AuthenticationResponseGrant(
        new ClaimsPrincipal(identity),
        new AuthenticationProperties { IsPersistent = true }
    );

您可以看到此link

© www.soinside.com 2019 - 2024. All rights reserved.