无法添加和获取自定义声明值
我正在使用带有身份2.0的mvc 5。 我想在应用程序上使用自定义声明值,但我得到null值。 我究竟做错了什么?
更新的代码
帐户控制器中的登录代码
if (!string.IsNullOrEmpty(model.UserName) && !string.IsNullOrEmpty(model.Password)) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var result = SignInManager.PasswordSignIn(model.UserName, model.Password, model.RememberMe, shouldLockout: false); //Generate verification token Dictionary acceccToken = null; if (SignInStatus.Success == 0) { var userDeatails = FindUser(model.UserName, model.Password).Result; if (userDeatails != null) acceccToken = GetTokenDictionary(model.UserName, model.Password, userDeatails.Id); } if (model.RememberMe) { HttpCookie userid = new HttpCookie("rembemberTrue", "1"); userid.Expires.AddDays(1); Response.Cookies.Add(userid); } else { HttpCookie userid = new HttpCookie("rembemberTrue", "0"); userid.Expires.AddDays(1); Response.Cookies.Add(userid); } #region custom claims var claims = new Claim[] { new Claim("urn:Custom:MasterUniqueId", Convert.ToString(Guid.NewGuid())) }; ClaimsIdentity identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); IAuthenticationManager authenticationManager = System.Web.HttpContext.Current.GetOwinContext().Authentication; authenticationManager.SignIn(identity);
Starup.Auth.cs
public void ConfigureAuth(IAppBuilder app) { // Configure the db context, user manager and signin manager to use a single instance per request app.CreatePerOwinContext(ApplicationDbContext.Create); app.CreatePerOwinContext(ApplicationUserManager.Create); app.CreatePerOwinContext(ApplicationSignInManager.Create); // Enable the application to use a cookie to store information for the signed in user // and to use a cookie to temporarily store information about a user logging in with a third party login provider // Configure the sign in cookie app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), Provider = new CookieAuthenticationProvider { // Enables the application to validate the security stamp when the user logs in. // This is a security feature which is used when you change a password or add an external login to your account. OnValidateIdentity = SecurityStampValidator.OnValidateIdentity( validateInterval: TimeSpan.FromMinutes(30), regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) }, SlidingExpiration = true, ExpireTimeSpan = TimeSpan.FromMinutes(60) });
另一个控制器
在这里,我试图获取声明值,但它显示为null
var identity = (ClaimsIdentity)User.Identity; var res= identity.FindFirst("urn:Custom:MasterUniqueId");
res为null
您应该在身份validation阶段添加这些声明。 请在此处检查类似的实现: 服务器端声明使用Owin身份validation进行缓存
在您的帐户控制器中,要获得有效的authenticationManager,您应该使用Request.GetOwinContext().Authentication
。 我很害怕System.Web.HttpContext.Current.GetOwinContext().Authentication
您提供一个新的authenticationManager而不是当前的Owin上下文
你有没有尝试过:
var res = identity.Claims.Where(c=>c.Type=="urn:Custom:MasterUniqueId").FirstOrDefault();
?
- 在实现自己的IUserStore时,类上的“可选”接口实际上是可选的吗?
- 为什么Asp.Net Identity IdentityDbContext是黑盒子?
- 在ASP.NET中的IdentityUserRole 2.0中获取角色名称
- 在ASP.NET WebApi 2中自定义承载令牌JSON结果
- 如何在ASP .NET MVC 5中为AspNetUser创建SecurityStamp
- ASP.Net Identity如何设置目标DB?
- Identity 3 MVC 6中的授权策略
- OWIN OpenID提供程序 – GetExternalLoginInfo()返回null
- ASP.net Identity 2.0退出另一个用户