MVC中的登录控件在帐户创建重定向后不显示用户名

时间:2009-08-21 19:11:56

标签: c# asp.net asp.net-mvc login

我正在使用自定义的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>

非常感谢任何帮助和见解。

3 个答案:

答案 0 :(得分:3)

你是对的。它没有显示用户名的原因是因为他们在创建帐户后重定向到主页时没有登录。更改以下行:

FormsAuth.SignIn(userName, false /* createPersistentCookie */);

为:

FormsAuth.SignIn(userName, true /* createPersistentCookie */);

答案 1 :(得分:0)

我做(而不是FormsAuth.SignIn())

if (Provider.ValidateUser(username, password)   
{  
     FormsAuth.SetAuthCookie(username, rememberMe);  
}  

其中记住我是一个变量,表示记住我复选框的值(true,false)

编辑:我刚看到这是在注册。我仍然使用.SetAuthCookie,但显然我没有使用.ValidateUser(在我的注册码中)。

答案 2 :(得分:0)

用户验证和自动登录的正常过程如下:

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