这个问题有点像结构/设计问题,因为我很难找到完成任务的最佳方法。
在我的MVC应用程序中,我使用DotNetOpenAuth (3.4)作为我的登录信息提供者,只使用cookies等的标准FormsAuthentication。
DB中的当前用户表有:
uniqueidentifier)
由于UserId是用户的明确标识符(他们应该能够在稍后的日期更改他们的OpenId提供程序),所以它是其他表链接到的关键(对于用户)。
这是当前的代码,在成功的身份验证中,它创建一个临时用户并重定向到creates。
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");
}现在问题的症结是:
response.ClaimedIdentifier吗?ClaimedIdentifier之外,没有任何与用户相关的数据。如果我一直在引用他们的ClaimedIdentifierUserId存储在cookie中,而不是在许多地方使用这个UserId,我如何从cookie中检索它,或者将它存储在其他更多的logical/useful?中
我有点喘不过气来,但我一直很难想出最好的办法
发布于 2010-02-04 06:35:30
1. 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. FormAuthentication.SetAuthCookie是表单身份验证的首选方法吗?还是有更好的方法?
对于MVC应用程序,肯定是这样的,因为您仍然可以从方法中返回您喜欢的ActionResult。
3.当我调用SetAuthCookie时,除了ClaimedIdentifier之外,没有任何与用户相关的数据。如果我一直提到他们的UserId,那么创建用户更好,然后将UserId存储在cookie中而不是ClaimedIdentifier中?
这听起来像是个人偏好。但是我通常会使用user_id,因为它可能会导致每次出现HTTP请求时都会更快地查找数据库,这就要求您查找任何用户信息。
4.如果我在许多地方使用该UserId,我如何从cookie中检索它,或者将其存储在其他更符合逻辑/更有用的地方?
FormsAuthentication确实提供了一种在加密cookie中存储更多信息的方法,而不仅仅是用户名,但它比您预期的要难。这个片段来自DotNetOpenAuth的web SSO RP示例:
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请求中获得这些额外数据,如下所示:
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
}
}https://stackoverflow.com/questions/2196965
复制相似问题