2012-09-12 63 views
1

嗨,大家好,我正在使用自定義會員提供商和自定義角色提供商。它正在使用這些正確登錄。我還實現了自己的Membership對象,這樣我就可以訪問其他用戶信息,並且每次更換頁面時都不需要加載所有數據,但是我目前無法正確使用它。下面是我的用戶對象:如何使用自定義的httpcontext用戶使用自定義會員/角色提供商

public class User : MembershipUser 
{ 
    [Required(ErrorMessage = "Username cannot be blank")] 
    [Display(Name = "User name")] 
    public string UserName { get; set; } 

    [Required(ErrorMessage = "Password cannot be blank")] 
    [DataType(DataType.Password)] 
    [Display(Name = "Password")] 
    public string Password { get; set; } 

    [Display(Name = "User ID")] 
    public long UserID { get; set; } 

    [Display(Name = "Family Name")] 
    [StringLength(50, ErrorMessage = "Family name cannot be longer than 50 characters")] 
    public string FamilyName { get; set; } 

    [Display(Name = "Given Name")] 
    [StringLength(50, ErrorMessage = "Given name cannot be longer than 50 characters")] 
    public string GivenName { get; set; } 

    public Company Company { get; set; } 

    public virtual IIdentity Identity { get; set; } 
} 

,並在用戶登錄時我調用下面的登錄方法:

[AllowAnonymous] 
    [HttpPost] 
    public ActionResult Login(User model, string returnUrl) 
    { 
     FormsAuthentication.SignOut(); 
     if(Membership.ValidateUser(model.UserName, model.Password)) 
     { 
      FormsAuthentication.SetAuthCookie(model.UserName, true); 
      return RedirectToAction("Index", ""); 
     } 
     ViewBag.Message = "Failed to login"; 
     return View(); 
    } 

但是,當我在指數看漲HttpContext.User它只是包含名稱/ ID,而不是休息我的用戶對象。我是否需要創建自定義​​對象?或者是標準過程將所有這些用戶信息存儲在HttpContext.Session對象內?或讓我的用戶延長System.Security.Principle.IPrinciple?甚至在Controller.TempData?或者我不熟悉的其他地方。我不想每次都要打到數據庫來加載用戶數據。

對不起,如果這些都是明顯的問題,我是相當新的Web開發,不知道做這些事情的通用方式是什麼。嘗試使用in build Authorize屬性。

回答

3

我通過實現我自己的身份做到了。這樣就可以很容易地添加儘可能多的屬性。下面是一個自定義屬性的friendlyName一個代碼示例

public class Identity : IIdentity 
    { 
     public Identity(int id, string name, string friendlyName, string roles) 
     { 
      this.ID = id; 
      this.Name = name; 
      this.FriendlyName = friendlyName; 
      this.Roles = roles; 
     } 



    public Identity(string name, string data) 
    { 
     if (string.IsNullOrWhiteSpace(data)) 
      throw new ArgumentException(); 

     string[] values = data.Split('|'); 
     if (values.Length != 3) 
      throw new ArgumentException(); 

     this.Name = name; 
     this.ID = Convert.ToInt32(values[0]); 
     this.FriendlyName = values[1]; 
     Roles = values[2]; 
    } 

    public string AuthenticationType 
    { 
     get { return "Custom"; } 
    } 

    public bool IsAuthenticated 
    { 
     get { return true; } 
    } 

    public override string ToString() 
    { 
     return FriendlyName; 
    } 

    public string GetUserData() 
    { 
     return string.Format("{0}|{1}|{2}", ID, FriendlyName, Roles); 
    } 


    public int ID { get; private set; } 
    public string Name { get; private set; } 
    public string FriendlyName { get; private set; } 
    public string Roles { get; private set; } 
} 

//in controller on login action: 
     Identity id = new Identity(user.ID, user.Username, "some friendly name", user.Roles); 
     DateTime expire = DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes); 
     FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(id.ID, user.Username, DateTime.Now, expire, false, id.GetUserData()); 
     string hashTicket = FormsAuthentication.Encrypt(ticket); 
     HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket); 
     HttpContext.Response.Cookies.Add(cookie); 

在Global.asax中你有:

public override void Init() 
     { 
      base.Init(); 
      PostAuthenticateRequest += new EventHandler(MvcApplication_PostAuthenticateRequest); 
     } 

    void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e) 
    { 
     HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; 
     if (authCookie != null) 
     { 
      FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); 
      if (authTicket == null || authTicket.Expired) 
       return; 

      Identity id = new Identity(authTicket.Name, authTicket.UserData); 
      Principal user = new Principal(id); 
      Context.User = user; 
      Thread.CurrentPrincipal = user; 
     } 
    } 
+0

我敢肯定,這可以在不同的/更好的方式來完成,但是這被證明是不夠好爲了我。 – MiBu

+0

謝謝,我會試試這個。我正在做類似的事情,但我遇到了問題,因爲我的用戶對象大約有1000個字符。所以我決定把我需要的其他用戶信息放入會話對象中。 – user1434177

+0

爲什麼'IsAuthenticated'返回true? – Dementic

相關問題