[英]Login control in MVC not displaying username after account creation redirect
我正在使用自定义的asp.net成员资格提供程序以及asp.net MVC示例应用程序随附的默认帐户控制器。 除了一件小事,一切都进行得很顺利:用户创建帐户并自动登录并重定向后,其用户名不会显示在通常的欢迎消息中。
我认为这可能是因为他们在发出请求时尚未登录。 一旦他们再次登录,他们的名字就会出现在顶部,所以我不认为这是会员资格提供者的错,但是我可能是错的。
asp.net MVC附带的注册和重定向控制器方法如下所示:
public ActionResult Register(string userName, string email, string password, string confirmPassword)
{
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
if (ValidateRegistration(userName, email, password, confirmPassword))
{
// Attempt to register the user
MembershipCreateStatus createStatus = MembershipService.CreateUser(userName, password, email);
if (createStatus == MembershipCreateStatus.Success)
{
FormsAuth.SignIn(userName, false /* createPersistentCookie */);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("_FORM", ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
return View();
}
顺便说一下,这是使用表单身份验证。
编辑:这是索引页使用的母版页。 默认的asp.net MVC应用程序附带的是Site.master:
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<div id="header">
<div id="title">
<h1>Internship Site</h1>
</div>
<div id="logindisplay">
<% Html.RenderPartial("LogOnUserControl"); %>
</div>
<div id="menucontainer">
<ul id="menu">
<li><%= Html.ActionLink("Home", "Index", "Home")%></li>
<li><%= Html.ActionLink("About", "About", "Home")%></li>
</ul>
</div>
</div>
<div id="main">
<noscript>Your browser does not support JavaScript!</noscript>
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
<div id="footer">
</div>
</div>
</div>
任何帮助和见解,不胜感激。
没错 未显示用户名的原因是,在创建帐户后重定向到首页时,他们尚未登录。 更改以下行:
FormsAuth.SignIn(userName, false /* createPersistentCookie */);
至:
FormsAuth.SignIn(userName, true /* createPersistentCookie */);
我这样做(而不是FormsAuth.SignIn())
if (Provider.ValidateUser(username, password)
{
FormsAuth.SetAuthCookie(username, rememberMe);
}
“记住我”是一个变量,指示“记住我”复选框的值(true,false)
编辑:我刚刚看到这是在注册中。 我仍然使用.SetAuthCookie,但显然我不使用.ValidateUser(在我的注册码中)。
用户验证和自动登录的正常过程如下:
public ActionResult Login(string userName, string password, bool persistent, string returnUrl)
{
if (returnUrl != null && returnUrl.IndexOf("/user/login", StringComparison.OrdinalIgnoreCase) >= 0)
returnUrl = null;
if (Membership.ValidateUser(userName, password))
{
FormsAuthentication.SetAuthCookie(userName, persistent);
if (!String.IsNullOrEmpty(returnUrl))
return this.Redirect(303, returnUrl);
else
return this.Redirect(303, FormsAuthentication.DefaultUrl);
}
else
TempData["ErrorMessage"] = "Login failed! Please make sure you are using the correct user name and password.";
return View();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.