尝试在MVC中扩展UserPrincipal无法正常工作

时间:2017-02-11 13:10:43

标签: c# asp.net-mvc model-view-controller userprincipal

我试图在MVC中扩展UserPrincipal方法,但我无法弄清楚它为什么不起作用。

我的代码:

interface ICustomPrincipal : IPrincipal
{
    Guid AuthId { get; set; }
    Role UserRole { get; set; }
}
public class CustomPrincipal : ICustomPrincipal
{
    public IIdentity Identity { get; private set; }
    public bool IsInRole(string role) { return false; }
    public CustomPrincipal(string email)
    {
        this.Identity = new GenericIdentity(email);
    }

    public Guid AuthId { get; set; }
    public Role UserRole { get; set; }
}

public class CustomPrincipalSerializeModel
{
    public Guid AuthId { get; set; }
    public Role UserRole { get; set; }
}

我的IdentityUser:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
    public Guid UserIdMatch { get; set; }
    public Role Role { get; set; }
}

因此,当创建用户时,他们会获得一个authId和一个角色。

当用户登录/注册时:

var user = db.Users.Include(i => i.Role).FirstOrDefault(i => i.UserName == model.Username);
CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
serializeModel.AuthId = user.UserIdMatch;
serializeModel.UserRole = user.Role;

JavaScriptSerializer serializer = new JavaScriptSerializer();
string userData = serializer.Serialize(serializeModel);

FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
    1,
    user.UserName,
    DateTime.Now,
    DateTime.Now.AddDays(1),
    false,
    userData
    );

string encTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
Response.Cookies.Add(faCookie);

最后在我的Global.asax我有以下代码:

protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
    HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

    if (authCookie != null)
    {
        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

        JavaScriptSerializer serializer = new JavaScriptSerializer();

        CustomPrincipalSerializeModel serializeModel = serializer.Deserialize<CustomPrincipalSerializeModel>(authTicket.UserData);

        CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
        newUser.AuthId = serializeModel.AuthId;

        HttpContext.Current.User = newUser;
    }
}

我的眼睛应该都很好,但是当我在任何页面上调用@((User as CustomPrincipalSerializeModel).AuthId)时,它会抛出以下错误:

  

对象引用未设置为对象的实例。

注意:我说调试登录时说。我没有收到任何错误,我可以看到cookie有正确的数据。

有人可以指出问题是什么吗?

1 个答案:

答案 0 :(得分:0)

您要将用户设置为CustomPrincipal,但您希望它是CustomPrincipalSerializeModel。由于这两者是无关的,这肯定不会起作用。

我打赌你的意思

@((User as CustomPrincipal).AuthId

因为AuthId属性似乎存在于两个类中。