这个问题有点像结构/设计问题,因为我无法找出执行任务的最佳方式。
在我的 MVC 应用程序中,我使用 DotNetOpenAuth (3.4) 作为我的登录信息提供程序,并且只使用FormsAuthentication
cookie 等标准。
数据库中的当前用户表有:
- 用户 ID(PK,唯一标识符)
- OpenId 标识符 (nvarchar(255))
- OpenIdDisplay (nvarchar(255))
- 显示名称 (nvarchar(50))
- 电子邮件 (nvarchar(50))
- 电话号码 (nvarchar(50))
由于 UserId 是用户的明确标识符(他们应该能够在以后更改其 OpenId 提供程序),因此它是其他表链接到的键(对于用户)。
这是当前代码,在成功验证后,会创建一个临时用户并重定向到 Create Action。
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
要针对用户存储的信息是否正确?是
FormAuthentication.SetAuthCookie
表单身份验证的首选方式吗?或者,还有更好的方法?当我调用 SetAuthCookie 时,除了
ClaimedIdentifier
. 如果我一直提到他们UserId
,创建用户是一个更好的主意,然后将其存储UserId
在 cookie 中而不是ClaimedIdentifier
?如果我在多个地方使用该 UserId,我如何从 cookie 中检索它,或者将其存储在其他更合乎逻辑/更有用的地方?
有点啰嗦,但我一直在尝试找出最好的方法来做到这一点/