asp.net-mvc – MVC 4 SimpleMembership – 登录后为什么WebSecu
发布时间:2020-12-16 04:32:22 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试在登录时设置cookie,并在登录后获取当前用户ID时遇到问题.在下面,intUserId是-1,WebSecurity.IsAuthenticated是false.这不是放置此代码的正确位置吗?在此之后,它会重定向到主页…所以不确定为什么这不是正确的地方. // POST: /Account/Login [Htt
我正在尝试在登录时设置cookie,并在登录后获取当前用户ID时遇到问题.在下面,intUserId是-1,WebSecurity.IsAuthenticated是false.这不是放置此代码的正确位置吗?在此之后,它会重定向到主页…所以不确定为什么这不是正确的地方.
// POST: /Account/Login [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(LoginModel model,string returnUrl) { if (ModelState.IsValid && WebSecurity.Login(model.UserName,model.Password,persistCookie: model.RememberMe)) { //TODO: set current company and facility based on those this user has access to int intUserId = WebSecurity.GetUserId(User.Identity.Name); int intUserId2 = WebSecurity.GetUserId(model.UserName); UserSessionPreferences.CurrentCompanyId = 1; UserSessionPreferences.CurrentFacilityId = 1; return RedirectToLocal(returnUrl); } // If we got this far,something failed,redisplay form ModelState.AddModelError("","The user name or password provided is incorrect."); return View(model); } 解决方法
仅登录设置表单身份验证cookie.
asp.net身份验证的工作方式是它必须读取cookie以设置对请求进行身份验证,但由于在启动“登录”页面时cookie不存在,因此框架对该用户一无所知. 重新加载页面,您会发现信息可用. 仅供参考,这对于SimpleMembership或WebSecurity来说并不是什么新鲜事,这是表单身份验证始终有效的方式. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – Request.GetOwinContext在单元测试中返回nu
- asp.net – 占位符/示例文本在用户的文本框中
- 使用UpdatePanel单击按钮后更新ASP.NET标签
- asp.net-mvc-2 – 电子商店中类别名称的MVC 2.0动态路由
- asp-classic – Response.Write和<%=%>
- 什么ASP.NET MVC项目文件应保存在存储库中?
- ASP.NET Ajax库死了吗?
- asp.net – Request.Url.AbsoluteUri和重写的URL
- asp.net-mvc – 在ASP.NET MVC中保护NullReferenceExceptio
- asp.net-mvc – Autofac,MVC(带ActionFilters),Web.Forms –
推荐文章
站长推荐
- asp.net-core – 在定位.NET Core时引用旧的(完整
- asp.net-mvc-4 – MVC Razor强类型视图中表行的回
- asp.net-mvc – 数据库中已经有一个名为“AspNet
- 来自asp.net会员身份验证的userid?
- 我应采取哪些措施来保护我的多层ASP.NET应用程序
- asp.net-core – compilationOptions.emitEntryP
- asp.net – 使用HTML’readonly =“readonly”’
- asp.net-mvc-4 – MVC脚手架 – 实现分页的快捷方
- .net – 为什么不直接控制你的引用?
- asp.net-mvc-2 – 用于实现CanExecute样式命令的
热点阅读