简体   繁体   中英

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. 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:

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:

<%@ 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())

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)

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).

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM