Asp.net mvc 3 登录MVC3后再次出现登录页面
你好 现在是我在项目中的问题。每次我登录它总是重定向再次在登录页面。我试图在我的控制器中设置一个断点,但碰巧的是,在第一次登录时,当我登录并单击控制器中的f10时,它会在所有条件下运行。但当我在第二秒登录时,它进入控制器以及该帐户的角色 我的代码: 客户控制员:Asp.net mvc 3 登录MVC3后再次出现登录页面,asp.net-mvc-3,authorization,roles,Asp.net Mvc 3,Authorization,Roles,你好 现在是我在项目中的问题。每次我登录它总是重定向再次在登录页面。我试图在我的控制器中设置一个断点,但碰巧的是,在第一次登录时,当我登录并单击控制器中的f10时,它会在所有条件下运行。但当我在第二秒登录时,它进入控制器以及该帐户的角色 我的代码: 客户控制员: public ActionResult LogOn(LogOnModel model, string returnUrl) { if (ModelState.IsValid)
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
if (Roles.IsUserInRole("Employer"))
{
return RedirectToAction("CustomerIndex", "Customer");
}
else if (Roles.IsUserInRole("Worker"))
{
return RedirectToAction("WorkerIndex", "Worker");
}
else if (Roles.IsUserInRole("Administrator"))
{
return RedirectToAction("ClientIndex", "Client");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
LogOn.cshtml:
@using (Html.BeginForm())
{
<div style="width: 500px;">
<fieldset>
<legend>Account Information</legend>
<div class="editor-label" style="padding-top: 20px">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
<p>
<input type="submit" value="Log On" class="styledButton" />
</p>
</fieldset>
</div>
}
@使用(Html.BeginForm())
{
帐户信息
@LabelFor(m=>m.UserName)
@Html.TextBoxFor(m=>m.UserName)
@Html.ValidationMessageFor(m=>m.UserName)
@LabelFor(m=>m.Password)
@Html.PasswordFor(m=>m.Password)
@Html.ValidationMessageFor(m=>m.Password)
@CheckBoxFor(m=>m.RememberMe)
@LabelFor(m=>m.RememberMe)
}
谢谢D这是因为用户在执行该操作时尚未在HttpContext中进行设置(此时您正在登录),因此对
角色.IsUserInRole
的调用将全部完成,即使登录的用户属于该角色
假设您正在使用一个您知道是管理员角色成员的用户进行日志记录。
如果在检查角色重定向之前将以下调试语句添加到LogOn
方法中,您可以自己查看
Debug.WriteLine("Controller user name: " + User.Identity.Name);
Debug.WriteLine("HttpContext user name: " + HttpContext.User.Identity.Name);
Debug.WriteLine(Roles.IsUserInRole("Administrator"));
您将在VisualStudio的调试输出窗口中看到,即使在使用loggedin并设置了auth cookie之后,用户名也将为空,并且IsUserInRole将返回false
但是,当您再次提交表单时,由于用户已经登录(您只是没有将其重定向到另一个页面),因此用户现在在HttpContext中设置。
因此,这次如果您在登录方法的开头添加这些调试行,您将在输出窗口中看到用户名,并且他是管理员
要解决您的问题,最简单的方法是使用允许您指定用户名的,而不是依赖HttpContext用户。您可以将用户角色检查为:
if (Roles.IsUserInRole(model.UserName, "Employer"))
{
return RedirectToAction("CustomerIndex", "Customer");
}
else if (Roles.IsUserInRole(model.UserName, "Worker"))
{
...
您也可以自己创建主体并将其设置为HttpContext.User,然后再检查用户角色:
var curIdentity = new System.Security.Principal.GenericIdentity(model.UserName);
var curPrincipal = new System.Security.Principal.GenericPrincipal(curIdentity, null /*userRoles*/);
HttpContext.User = curPrincipal;
if (Roles.IsUserInRole("Employer"))
{
return RedirectToAction("CustomerIndex", "Customer");
}
else if (Roles.IsUserInRole("Worker"))
{
...
不过,我会保持简单,只需将用户名传递给IsUserInRole方法。对于应用程序中的任何其他操作,您可以依赖于在HttpContext中自动为您设置的用户。这是因为在执行该操作时用户尚未在HttpContext中设置(因为您当时正在登录),因此对
角色.IsUserInRole
的调用将全部,即使登录的用户属于该角色
假设您正在使用一个您知道是管理员角色成员的用户进行日志记录。
如果在检查角色重定向之前将以下调试语句添加到LogOn
方法中,您可以自己查看
Debug.WriteLine("Controller user name: " + User.Identity.Name);
Debug.WriteLine("HttpContext user name: " + HttpContext.User.Identity.Name);
Debug.WriteLine(Roles.IsUserInRole("Administrator"));
您将在VisualStudio的调试输出窗口中看到,即使在使用loggedin并设置了auth cookie之后,用户名也将为空,并且IsUserInRole将返回false
但是,当您再次提交表单时,由于用户已经登录(您只是没有将其重定向到另一个页面),因此用户现在在HttpContext中设置。
因此,这次如果您在登录方法的开头添加这些调试行,您将在输出窗口中看到用户名,并且他是管理员
要解决您的问题,最简单的方法是使用允许您指定用户名的,而不是依赖HttpContext用户。您可以将用户角色检查为:
if (Roles.IsUserInRole(model.UserName, "Employer"))
{
return RedirectToAction("CustomerIndex", "Customer");
}
else if (Roles.IsUserInRole(model.UserName, "Worker"))
{
...
您也可以自己创建主体并将其设置为HttpContext.User,然后再检查用户角色:
var curIdentity = new System.Security.Principal.GenericIdentity(model.UserName);
var curPrincipal = new System.Security.Principal.GenericPrincipal(curIdentity, null /*userRoles*/);
HttpContext.User = curPrincipal;
if (Roles.IsUserInRole("Employer"))
{
return RedirectToAction("CustomerIndex", "Customer");
}
else if (Roles.IsUserInRole("Worker"))
{
...
不过,我会保持简单,只需将用户名传递给IsUserInRole方法。对于应用程序中的任何其他操作,您可以依赖在HttpContext中自动为您设置的用户。@VincentGumera,很高兴能提供帮助!:)为你竖起大拇指!:)@VincentGumera,很高兴能帮上忙!:)为你竖起大拇指!:)