2010-02-04 162 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