Mvc Identity Longin ClaimsPrincipal值始终为null

时间:2017-08-21 15:24:53

标签: asp.net-mvc c#-4.0 asp.net-identity principal

这是我的登录控制器

public class AccountController : BaseController
{
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginViewModel model, string returnUrl)
    {                                         
      var identity = new ClaimsIdentity(new[] 
      {
         new Claim(ClaimTypes.Name, "Huang"),
         new Claim(ClaimTypes.NameIdentifier, user.Vaild.ToString()),
         new Claim(ClaimTypes.Email,"xxx@gmail.com"),
         new Claim(ClaimTypes.MobilePhone,"123456"), 
      },DefaultAuthenticationTypes.ApplicationCookie);


  IdentityTool.Authentication(AuthenticationManager,identity,rememberMe:true);

     // userEmail and userPhone  always null and I got null exception
        var userEmail = CurrentUser.Email;
        var userPhone = CurrentUser.MobilePhone;  

    return View(model);
   }
}

public class BaseController : Controller
{
    public AppUser CurrentUser
    {
        get { return new AppUser(this.User as System.Security.Claims.ClaimsPrincipal);}

    }
}

IdentityConfig

public class IdentityTool
{
 public static void Authentication(IAuthenticationManagerauthenticationManager, ClaimsIdentity identity, bool rememberMe)
     {
        if (rememberMe)
        {
            authenticationManager.SignIn(new AuthenticationProperties()
            {
                IsPersistent = true,
                ExpiresUtc = DateTimeOffset.UtcNow.AddDays(14)
            }, identity);
        }
        else
        {
            authenticationManager.SignIn(new AuthenticationProperties()
            {
                IsPersistent = false
            }, identity);
        }
    }
}

//Custom property
public class AppUser : ClaimsPrincipal
{        
    public AppUser(ClaimsPrincipal principal) : base(principal)
    {
    }
    public string Email
    {
        get
        {
            return this.FindFirst(ClaimTypes.Email).Value;
        }
    }

    public string MobilePhone
    {
        get
        {
            return this.FindFirst(ClaimTypes.MobilePhone).Value;
        }
    }
 }

这是我的初创公司

public void ConfigureAuth(IAppBuilder app)
{
    AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;                     
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType =DefaultAuthenticationTypes.ApplicationCookie, 
        LoginPath = new PathString("/Account/Login"),
        Provider = new CookieAuthenticationProvider
        {

            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromMinutes(30),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        }
    });            
    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

然后我在“CurrentUser.Email”和“CurrentUser.MobilePhone”上的null异常总是为null。请帮我修理一下。我真的很感激。我问很多人,但没有人可以解决它。如果你有一些建议修改一些代码。可能有用。请让我知道。吹图是我的例外细节。对不起,图片中有一些中文。^^

This is my exception detail hope help

0 个答案:

没有答案