2010-02-04 80 views
17

這個問題是一個結構/設計問題,因爲我在解決執行任務的最佳方式時遇到了問題。存儲DotNetOpenAuth信息和用戶信息檢索

在我的MVC應用程序,我使用DotNetOpenAuth(3.4)作爲我的登錄信息提供者,只是使用標準FormsAuthentication餅乾等

在DB當前用戶表有:

  • 用戶ID(PK,唯一標識符)
  • OpenIdIdentifier(nvarchar的(255))
  • OpenIdDisplay(nvarchar的(255))
  • DISPLAYNAME(nvarchar的(50))
  • 電子郵件(爲nvarchar(50))
  • ******中國(爲nvarchar(50))

由於用戶ID是用戶的明確標識(他們應該能夠在日後改變自己的OpenID提供商),這是其他錶鏈接到(用戶)的關鍵。

這是當前的代碼,在成功的身份驗證上創建臨時用戶並重定向到創建操作。

 switch (response.Status) 
     { 
      case AuthenticationStatus.Authenticated: 

       FormsAuthentication.SetAuthCookie(response.ClaimedIdentifier, false); 

       var users = new UserRepository(); 
       if (!users.IsOpenIdAssociated(response.ClaimedIdentifier)) 
       { 
        var newUser = new DueDate.Models.User(); 
        newUser.OpenIdIdentifer = response.ClaimedIdentifier; 
        newUser.OpenIdDisplay = response.FriendlyIdentifierForDisplay; 

        TempData["newUser"] = newUser; 

        return this.RedirectToAction("Create"); 
       } 

現在提出這個問題的癥結所在:

  1. 是在response.ClaimedIdentifier正確的資料片將被存儲對用戶?

  2. FormAuthentication.SetAuthCookie形成驗證的首選方式是?或者,還有更好的方法?

  3. 當我打電話給SetAuthCookie時,除了ClaimedIdentifier之外,沒有與用戶有關的數據。如果我一直指他們的UserId,創建用戶是一個更好的主意,那麼將該UserId存儲在cookie中而不是ClaimedIdentifier

  4. 如果我在很多地方使用該UserId,我該如何從cookie中檢索它,或者將它存儲在其他更合理/有用的位置?

有點長篇大論,但我一直有麻煩試圖找出要做到這一點/

回答

26

的最佳方式1.Is的response.ClaimedIdentifier正確的資料片被保存對用戶?

。並確保將其存儲在數據庫中的列區分大小寫。這是一個表格模式,演示如何確保它是區分大小寫的。這來自DotNetOpenAuth項目模板的數據庫模式。指定歸類的「CS」位表示區分大小寫。

CREATE TABLE [dbo].[AuthenticationToken] (
    [AuthenticationTokenId] INT   IDENTITY (1, 1) NOT NULL, 
    [UserId]     INT   NOT NULL, 
    [OpenIdClaimedIdentifier] NVARCHAR (250) COLLATE SQL_Latin1_General_CP1_CS_AS NOT NULL, 
    [OpenIdFriendlyIdentifier] NVARCHAR (250) NULL, 
    [CreatedOn]    DATETIME  NOT NULL, 
    [LastUsed]     DATETIME  NOT NULL, 
    [UsageCount]    INT   NOT NULL 
); 

2.Is FormAuthentication.SetAuthCookie到表單認證的優選方法是什麼?或者,還有更好的方法?

對於MVC應用程序,它肯定是,因爲您仍然可以從該方法返回您首選的ActionResult

3.當我調用SetAuthCookie時,除了ClaimedIdentifier之外,沒有與用戶有關的數據。如果我持續引用他們的UserId,創建用戶是一個更好的主意,那麼將該UserId存儲在Cookie中而不是ClaimedIdentifier中?

這聽起來像個人偏好。但我通常會使用user_id,因爲每次發出HTTP請求時都會導致數據庫查找更快,這​​需要查找任何用戶信息。

4.如果我在很多地方使用該UserId,我該如何從cookie中檢索它,或者將它存儲在其他更合乎邏輯/有用的位置?

FormsAuthentication 確實提供了一種方法來存儲不僅僅是用戶名的加密的cookie的更多信息,但它比你希望用它更難。這個片斷出來DotNetOpenAuth的Web SSO RP樣本:

var cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; 
if (cookie != null) { 
    var ticket = FormsAuthentication.Decrypt(cookie.Value); 
    if (!string.IsNullOrEmpty(ticket.UserData)) { 
     // do something cool with the extra data here 
    } 
} 
+0

非常感謝你,一個偉大的答案,太多:

const int TimeoutInMinutes = 100; // TODO: look up the right value from the web.config file var ticket = new FormsAuthenticationTicket( 2, // magic number used by FormsAuth response.ClaimedIdentifier, // username DateTime.Now, DateTime.Now.AddMinutes(TimeoutInMinutes), false, // "remember me" "your extra data goes here"); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket)); Response.SetCookie(cookie); Response.Redirect(Request.QueryString["ReturnUrl"] ?? FormsAuthentication.DefaultUrl); 

然後你就可以在這個未來的HTTP請求的額外的數據得到讚賞:) – 2010-02-04 07:18:29