声明长时间未保留在身份中,但用户已通过身份验证

时间:2019-12-05 17:45:42

标签: authentication model-view-controller asp.net-identity claims

我正在使用asp.net MVC身份并想在身份中添加自定义声明。 只要用户使用站点,这就可以很好地工作。在长时间不使用声明后,声明消失了,在调试时,我没有在Identity的声明集合中看到自定义声明,但用户仍通过了身份验证。

任何人都可以在这里帮助我,为什么会这样以及如何保留自定义声明。

这是我的代码-

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            var identity = await user.GenerateUserIdentityAsync(UserManager);
            // added custom claims 
            identity.AddClaim(new Claim(CustomClaimTypes.A, "Value1"));
            identity.AddClaim(new Claim(CustomClaimTypes.B, "Value2"));
            identity.AddClaim(new Claim(CustomClaimTypes.C, "Value3"));

            AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
        }

在ApplicationUser类中-

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
        {
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);    

            return userIdentity;
        } 

0 个答案:

没有答案