asp.net-mvc – MVC 4 SimpleMembership – 登录后为什么WebSecurity.Cur
发布时间:2020-05-24 10:21:21 所属栏目:asp.Net 来源:互联网
导读:我正在尝试在登录时设置cookie,并在登录后获取当前用户ID时遇到问题.在下面,intUserId是-1,WebSecurity.IsAuthenticated是false.这不是放置此代码的正确位置吗?在此之后,它会重定向到主页…所以不确定为什么这不是正确的地方. // POST: /Account/Login [Http
|
我正在尝试在登录时设置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来说并不是什么新鲜事,这是表单身份验证始终有效的方式. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- 实体框架 – 带有实体框架的MVC 3和ASP.Net脚手架
- asp.net – 如何删除日历的最后一周
- 在ASP.NET中开发SharePoint Web部件
- asp.net-mvc – asp.net MVC:禁用一个TextBox
- asp.net – 无法将索引35处的字节[FC]从指定的代
- asp.net-core – asp.net核心身份中的多个SubDom
- asp.net-mvc – 如何调试此错误:’无法找到iise
- asp.net-mvc – Html.Label和Html.Display有什么
- 用asp.net MVC局部视图加载jquery
- asp.net-mvc-2 – Asp.net MVC标签
热点阅读
