[英]Login control in MVC not displaying username after account creation redirect
I'm working with a custom asp.net membership provider in conjunction with the default Account controller that comes with the asp.net MVC example app. 我正在使用自定义的asp.net成员资格提供程序以及asp.net MVC示例应用程序随附的默认帐户控制器。 Everything is going smoothly except for one tiny thing: After a user creates an account and is automatically logged in and redirected, their username is not displayed with the usual welcome message.
除了一件小事,一切都进行得很顺利:用户创建帐户并自动登录并重定向后,其用户名不会显示在通常的欢迎消息中。
I thought perhaps this was because they were not logged in at the time they made the request. 我认为这可能是因为他们在发出请求时尚未登录。 Once they log in again their name appears at the top, so I don't think it's the membership providers fault, but I could be wrong.
一旦他们再次登录,他们的名字就会出现在顶部,所以我不认为这是会员资格提供者的错,但是我可能是错的。
The register and redirect controller method that ships with asp.net MVC looks like this: 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();
}
This is using forms authentication by the way. 顺便说一下,这是使用表单身份验证。
Edit: Here is the Master page used by the Index page. 编辑:这是索引页使用的母版页。 It's the Site.master that ships with the default asp.net MVC application:
默认的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>
Any help and insight greatly appreciated. 任何帮助和见解,不胜感激。
You're correct. 没错 The reason it's not showing the user's name is because they are not logged in at the time of the redirect to the homepage after account creation.
未显示用户名的原因是,在创建帐户后重定向到首页时,他们尚未登录。 Change the following line:
更改以下行:
FormsAuth.SignIn(userName, false /* createPersistentCookie */);
to: 至:
FormsAuth.SignIn(userName, true /* createPersistentCookie */);
I do (instead of FormsAuth.SignIn()) 我这样做(而不是FormsAuth.SignIn())
if (Provider.ValidateUser(username, password)
{
FormsAuth.SetAuthCookie(username, rememberMe);
}
where remember me is a variable indicating the value of the remember me checkbox (true, false) “记住我”是一个变量,指示“记住我”复选框的值(true,false)
Edit: I just saw that this is in registration. 编辑:我刚刚看到这是在注册中。 I still use the .SetAuthCookie, but obviously I don't do the .ValidateUser (in my registration code).
我仍然使用.SetAuthCookie,但显然我不使用.ValidateUser(在我的注册码中)。
The normal process of user validation and automated login goes as following: 用户验证和自动登录的正常过程如下:
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.