简体   繁体   中英

ASP.NET Core Identity - How to add new claims after initial login

I am trying to add new claims to the Identity after the initial login based on certain data that is queried from database. The new claims that I am adding not persisting for subsequent requests.

This is how i'm setting/Adding claims in ASP.NET MVC

public static void UpdateClaim(IPrincipal principal, string key, string value)
        {
            var identity = principal.Identity as ClaimsIdentity;
            if (identity == null)
                return;

            // check for existing claim and remove it
            var existingClaim = identity.FindFirst(key);
            if (existingClaim != null)
                identity.RemoveClaim(existingClaim);

            // add new claim
            identity.AddClaim(new Claim(key, value));
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
            authenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant(new ClaimsPrincipal(identity), new AuthenticationProperties() { IsPersistent = true });
        }

Is there is similar way in ASP.NET Core 2.0 to persist newly added claims. Appreciate any ideas on this.

声明持续存在,但仅在登录时加载。如果您更改声明或角色等内容,您必须注销用户,然后自动重新登录或提示他们重新进行身份验证以更新声明。

如果您只是在 asp 核心中的 Claims 标识对象中添加声明,则声明不会持续存在,您将不得不使用 iClaimsTransformation 或 ClaimAction,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM