asp.net – 存储DotNetOpenAuth信息和用户信息检索
这个问题是一个结构/设计问题,因为我无法做出最好的方式来执行任务。
在我的MVC应用程序中,我使用的是DotNetOpenAuth(3.4)作为我的登录信息提供者,并且只使用标准的FormsAuthentication for cookies等。 DB中的当前用户表有: > UserId(PK,uniqueidentifier) 由于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是否正确存储对用户的信息? 有点长,但我一直在努力找出最好的方法来做这个/ 解决方法
是。并确保将其存储在数据库中的列区分大小写。这是一个表模式,演示如何确保它区分大小写。这来自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 );
对于MVC应用,它肯定是,因为您仍然可以从该方法返回您首选的ActionResult。
这听起来像个人喜好。但是我通常会使用user_id,因为每次HTTP请求都需要您查找任何用户信息,因此可能会导致更快的数据库查找。
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 } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- WPAD查询从ASP.NET调用webservice
- asp.net-mvc – 确保视图存在
- asp.net – 在新线程中使用Ninject
- EventBus/EventQueue 再思考
- asp.net-mvc – 澄清在ASP.NET MVC中使用ELAMH
- asp.net – 检查控制启动的AJAX请求
- asp.net-mvc-3 – 可以用“@:@”修复的MVC4剃刀的可能的突
- asp.net – 在发布Web应用程序时如何包含未引用的dll?
- asp.net-mvc – 我应该为ASP.Net MVC项目使用什么ORM?
- asp.net – 在.NET 4.5 Azure部署中找不到编译器可执行文件
- 我可以在ASP.NET中使用C#获取时间参数的本地化缩
- asp.net-mvc – 如何从HttpContext获取JWT(使用O
- 单元测试传统ASP.NET Webforms应用程序
- ASP.NET MVC 3自定义身份验证/授权
- asp.net-mvc – mvc web应用程序应该是3层吗?
- 互联网级监控系统必备-时序数据库之Influxdb集群
- asp.net – 基于web.config的url重写的绝对最小内
- asp.net-mvc – .NET MVC是否有强类型的Redirect
- asp.net – 当用户使表单失效时,如何点击取消按钮
- asp.net-mvc-3 – Razor in-line if语句不工作?