15

我正在使用带有标识2.0的mvc 5。我想在应用程序上使用自定义声明值,但是我得到空值。我在哪里做错了?请帮助帐户控制我无法添加和获取自定义声明值

更新代码

登入码

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<string, string> 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>(ApplicationUserManager.Create); 
      app.CreatePerOwinContext<ApplicationSignInManager>(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<ApplicationUserManager, ApplicationUser>(
         validateInterval: TimeSpan.FromMinutes(30), 
         regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) 
       }, 
       SlidingExpiration = true, 
       ExpireTimeSpan = TimeSpan.FromMinutes(60) 
      }); 

....... ...

另一个控制器

我在这里试图获取该要求值,但它显示空

var identity = (ClaimsIdentity)User.Identity; 
var res= identity.FindFirst("urn:Custom:MasterUniqueId"); 

水库是空

+0

你可以显示你正在使用的完整登录代码?该代码是否正确异步的方法?另外,你在启动时是否缺少'app.UseCookieAuthentication(...)'? –

+0

@BrendanGreen,我已更新我的问题,请看看 –

+0

你可以看到'User.Identity.IsAuthenticated'和'User.Identity.Name'的输出是什么? –

回答

-1

你试过:

var res = identity.Claims.Where(c=>c.Type=="urn:Custom:MasterUniqueId").FirstOrDefault(); 

+0

是的,我已经尝试过,但仍然是空的。当时执行的登录代码MasterUniqueId在属性中可见。但在方法范围之后它刚刚消失 –

0

在您的帐户控制器中,要获得有效的验证管理器,您应该使用Request.GetOwinContext().Authentication。我担心System.Web.HttpContext.Current.GetOwinContext().Authentication让你一个新的身份验证管理器,而不是当前的Owin上下文一个