首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >存储DotNetOpenAuth信息和用户信息检索

存储DotNetOpenAuth信息和用户信息检索
EN

Stack Overflow用户
提问于 2010-02-04 02:27:10
回答 1查看 2.3K关注 0票数 17

这个问题有点像结构/设计问题,因为我很难找到完成任务的最佳方法。

在我的MVC应用程序中,我使用DotNetOpenAuth (3.4)作为我的登录信息提供者,只使用cookies等的标准FormsAuthentication

DB中的当前用户表有:

uniqueidentifier)

  • OpenIdIdentifier (nvarchar(255))

  • OpenIdDisplay (nvarchar(255))

  • Displayname (nvarchar(50))

  • Email (nvarchar(50))

  • PhoneNumber (nvarchar(50))

  • UserId (PK )

由于UserId是用户的明确标识符(他们应该能够在稍后的日期更改他们的OpenId提供程序),所以它是其他表链接到的关键(对于用户)。

这是当前的代码,在成功的身份验证中,它创建一个临时用户并重定向到creates。

代码语言:javascript
复制
        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. 是表单身份验证的首选方法吗?或者有更好的方法?
  3. 当我调用SetAuthCookie时,除了ClaimedIdentifier之外,没有任何与用户相关的数据。如果我一直在引用他们的ClaimedIdentifier
  4. If,那么创建用户是一个更好的主意,那么将这个UserId存储在cookie中,而不是在许多地方使用这个UserId,我如何从cookie中检索它,或者将它存储在其他更多的logical/useful?

我有点喘不过气来,但我一直很难想出最好的办法

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-02-04 06:35:30

1. response.ClaimedIdentifier是否是针对用户存储的正确信息?

是。并确保将其存储在数据库中的列区分大小写。下面是一个表架构,演示如何确保它区分大小写。这来自于DotNetOpenAuth项目模板的数据库模式。指定排序规则的"CS“位表示区分大小写。

代码语言:javascript
复制
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. FormAuthentication.SetAuthCookie是表单身份验证的首选方法吗?还是有更好的方法?

对于MVC应用程序,肯定是这样的,因为您仍然可以从方法中返回您喜欢的ActionResult

3.当我调用SetAuthCookie时,除了ClaimedIdentifier之外,没有任何与用户相关的数据。如果我一直提到他们的UserId,那么创建用户更好,然后将UserId存储在cookie中而不是ClaimedIdentifier中?

这听起来像是个人偏好。但是我通常会使用user_id,因为它可能会导致每次出现HTTP请求时都会更快地查找数据库,这就要求您查找任何用户信息。

4.如果我在许多地方使用该UserId,我如何从cookie中检索它,或者将其存储在其他更符合逻辑/更有用的地方?

FormsAuthentication确实提供了一种在加密cookie中存储更多信息的方法,而不仅仅是用户名,但它比您预期的要难。这个片段来自DotNetOpenAuth的web SSO RP示例:

代码语言:javascript
复制
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请求中获得这些额外数据,如下所示:

代码语言:javascript
复制
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
    }
}
票数 26
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2196965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档