2017-02-11 8 views
0

MVPで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と役割を取得します。

ユーザのログイン/登録:私のGlobal.asaxで最後に

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); 

私は次のコードを持っている:それはすべて私の目には問題ないはずです

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)を呼び出すときどのページでも、次のエラーがスローされます。

Object reference not set to an instance of an object.

注:let sa y私はログインをデバッグします。私は何のエラーも表示されず、私はクッキーが正しいデータを持っているのを見ることができます。

誰かが問題点を指摘できますか?

答えて

1

ユーザーをCustomPrincipalに設定していますが、それにはCustomPrincipalSerializeModelが必要です。 2つは無関係なので、これは確実に機能しません。

私はAuthIdプロパティは、両方のクラスに存在するように見えるので、あなたが

@((User as CustomPrincipal).AuthId 

を意味賭けます。

+0

あなたの正しい。今すぐAuthIdが動作します。しかし、ロールのプロパティはありません任意のアイデア?決して気にせず、見つけました。 –

関連する問題