Ala*_*tts 17 database asp.net dotnetopenauth
这个问题是一个结构/设计问题,因为我无法找到执行任务的最佳方法.
在我的MVC应用程序中,我使用DotNetOpenAuth(3.4)作为我的登录信息提供者,只使用FormsAuthenticationcookie 的标准等.
DB中的当前用户表具有:
由于UserId是用户的明确标识符(他们应该能够在以后更改其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");
}
Run Code Online (Sandbox Code Playgroud)
现在问题的症结在于:
是否response.ClaimedIdentifier存储了针对用户的正确信息?
是FormAuthentication.SetAuthCookie表单身份验证的首选方法吗?或者,还有更好的方法?
当我调用SetAuthCookie时,除了用户之外没有与用户相关的数据ClaimedIdentifier.如果我一直提到他们UserId,创建用户是个更好的主意,那么将其存储UserId在cookie而不是ClaimedIdentifier?
如果我在很多地方使用UserId,我如何从cookie中检索它,或者将它存储在其他更合乎逻辑/有用的地方?
有点长的啰嗦,但我一直在努力找到最好的方法来做这个/
And*_*ott 26
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
);
Run Code Online (Sandbox Code Playgroud)
2. FormAuthentication.SetAuthCookie表单身份验证的首选方式是什么?或者,还有更好的方法?
对于MVC应用程序肯定是,因为您仍然可以ActionResult从方法返回您的首选项.
3.当我调用SetAuthCookie时,除了ClaimedIdentifier之外,没有与用户相关的数据.如果我一直在指他们的UserId,最好是创建用户,然后将UserId存储在cookie而不是ClaimedIdentifier中?
这听起来像个人偏好.但我通常会使用user_id,因为每次发出HTTP请求时,它可能会导致更快的数据库查找,这需要您查找任何用户信息.
4.如果我在很多地方使用UserId,我如何从cookie中检索它,或者将它存储在其他更合乎逻辑/有用的地方?
FormsAuthentication 确实提供了一种在其加密cookie中存储更多信息的方法,而不仅仅是用户名,但它比您期望使用它更难.这个片段来自DotNetOpenAuth的网络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);
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用以下方法获取未来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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2246 次 |
| 最近记录: |