2011-07-06 128 views
5

我堅持使用自定義iprincpal和iidentity對象。我現在花了一天的時間來搜索如何實施這些權利,並用更多的信息擴展它。在MVC中使用自定義成員資格和角色提供者實現IPrincipal和IIdentity

我想擴展信息@Context.User.Identity.Name自定義變量如全名或其他東西。

編輯:現在我得到了下面的代碼,但如果我嘗試閱讀@((CustomPrincipal)Context.User.Identity).Nachname我得到一個錯誤System.Web.Security.FormsIdentity不能被強制轉換爲CustomPrincipal

任何想法?

public class CustomPrincipal : GenericPrincipal 
{ 
    public CustomPrincipal(IIdentity identity, String[] roles) : base(identity, roles){ 

    } 
    public String Vorname { get; set; } 
    public String Nachname { get; set; } 
} 

AccountModel:

public class FormsAuthenticationService : IFormsAuthenticationService 
{ 
    public void SignIn(string userName, bool createPersistentCookie) 
    { 
     if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Der Wert darf nicht NULL oder leer sein.", "userName"); 
     // Grab user information to insert 
     KIMembershipUser membershipUser = (KIMembershipUser)Membership.GetUser(userName); 
     var customInfo = String.Format("{0}|{1}", membershipUser.Vorname, membershipUser.Nachname); 
     // Create and encrypt the ticket 
     var ticket = new FormsAuthenticationTicket(
      2, // Version number 
      userName, // Username 
      DateTime.Now, // Issue date 
      DateTime.Now.AddMinutes(30), // Expiration date 
      createPersistentCookie, // Is it persistent? 
      customInfo // User data 
     ); 
     var encTicket = FormsAuthentication.Encrypt(ticket); 
     // Store the ticket into a cookie 
     var cookie = FormsAuthentication.GetAuthCookie(FormsAuthentication.FormsCookieName,createPersistentCookie); 
     cookie.Value = encTicket; 
     // Append the cookie to the response 
     HttpContext.Current.Response.Cookies.Add(cookie); 

     //FormsAuthentication.SetAuthCookie(userName, createPersistentCookie); 
    } 

    public void SignOut() 
    { 
     FormsAuthentication.SignOut(); 
    } 
} 

的Global.asax:

protected void Application_PostAuthenticateRequest(){ 
     // Collect current security information 
     var principal = HttpContext.Current.User as RolePrincipal; 
     if (principal == null) 
      return; 
     var identity = principal.Identity as FormsIdentity; 
     if (identity == null) 
      return; 
     var roles = principal.GetRoles(); 
     // Extract user data in the authentication ticket 
     var customInfo = identity.Ticket.UserData; 
     var tokens = customInfo.Split('|'); 
     // Build a richer principal object 
     var CustomPrincipal = new CustomPrincipal(identity, roles){ 
      Vorname = tokens[0], 
      Nachname = tokens[1] 
     }; 
     // Store the new principal in the HttpContext 
     HttpContext.Current.User = CustomPrincipal; 
    } 
+0

什麼是延伸的GenericPrincipal和GenericIdentity類的問題? –

+0

我不知道有這些對象。如何擴展它們? – float

+0

您可能不需要擴展(繼承)它們,最簡單的方法是構建它們中的每一個的新實例:http://msdn.microsoft.com/en-us/library/y9dd5fx0(v=VS .100).aspx –

回答

4

使用(CustomPrincipal)Context.User).Nachname而不是(CustomPrincipal)Context.User.Identity).Nachname

相關問題