2010-11-18 23 views
6

Tôi bị mất một chút khi sử dụng Xác thực với MVC ...Tùy chọn tốt nhất để xác thực tùy chỉnh bằng ASP .NET MVC (Bộ nhớ cache, Cookie ...)

Tôi đang tìm tùy chọn tốt nhất để sử dụng trong Thương mại điện tử trang web, nơi hiệu suất là ưu tiên hàng đầu ...

hai tùy chọn Tôi đang tìm kiếm cho đến bây giờ là:

  • Tạo một FormsAuthenticationTicket và mã hóa nó thành một Cookie, giống như khai báo ở đây: Cookie implementation
  • cache dữ liệu xác thực, như thế:

    protected void Application_AuthenticateRequest(object sender, EventArgs e) 
    { 
        if (HttpContext.Current.User != null) 
        { 
         if (HttpContext.Current.User.Identity.IsAuthenticated) 
         { 
          if (HttpContext.Current.User.Identity is FormsIdentity) 
          { 
           // Get Forms Identity From Current User 
           FormsIdentity id = FormsIdentity)HttpContext.Current.User.Identity; 
           // Create a custom Principal Instance and assign to Current User (with caching) 
           Customer principal = (Customer)HttpContext.Current.Cache.Get(id.Name); 
           if (principal == null) 
           { 
            // Create and populate your Principal object with the needed data and Roles. 
            principal = MyBusinessLayerSecurityClass.CreatePrincipal(id, id.Name);        
            HttpContext.Current.Cache.Add(
            id.Name, 
            principal, 
            null, 
            System.Web.Caching.Cache.NoAbsoluteExpiration, 
            new TimeSpan(0, 30, 0), 
            System.Web.Caching.CacheItemPriority.Default, 
            null); 
           } 
           HttpContext.Current.User = principal; 
          } 
         } 
        } 
    } 
    

Caching sample here

gì các bạn nghĩ sao?

Cảm ơn

Trả lời

4

Một hơn MVCish cách để đạt được điều này là để viết một tùy chỉnh AuthorizeAttribute và thực hiện điều này trong một overriden OnAuthorization phương pháp thay vì sử dụng Application_AuthenticateRequest.

Điều này đang được nói rằng tôi nghĩ rằng việc triển khai của bạn khá tốt. Thay vì lưu trữ thông tin bổ sung vào bộ đệm, bạn có thể lưu trữ nó trong phần userData của vé xác thực nếu thông tin này không quá lớn. Cả hai phương pháp đều khả thi. Nếu bạn quyết định đi với bộ nhớ đệm, tôi sẽ khuyên bạn nên tải nó xuống máy chủ lưu trữ dành riêng cho bộ nhớ cache thay vì lưu trữ nó trong bộ nhớ của máy chủ web.

Các vấn đề liên quan