简体   繁体   English

登录后显示用户名不起作用

[英]Displaying the username after login not working

I am having trouble displaying the username after login on my index page.在我的索引页面上登录后,我无法显示用户名。

The code I have for Logging in:我用于登录的代码:

protected void btnLoginButton_Click(object sender, EventArgs e)
    {
        if (Membership.ValidateUser(UserName.Text, Password.Text))
        {
            // Log the user into the site
            Response.Redirect("~/Index.aspx");
        }
        // If we reach here, the user's credentials were invalid
        InvalidCredentialsMessage.Visible = true;
    }

Then in my Index Page:然后在我的索引页面中:

<asp:Label runat="server" ID="WelcomeBackMessage">Label</asp:Label>

Code Behind:代码背后:

protected void Page_Load(object sender, EventArgs e)
    {
        WelcomeBackMessage.Text = User.Identity.Name.ToString();
    }

The problem is that there are no errors and it is not displaying the name at all.问题是没有错误,并且根本不显示名称。

Thanks谢谢

You simply forgot to actually login your user.您只是忘记实际登录您的用户。 All you do is validating the users credentials and then do a redirect.您所做的只是验证用户凭据,然后进行重定向。 So try to add the following line in your if-statement:因此,请尝试在 if 语句中添加以下行:

protected void LoginButton_Click(object sender, EventArgs e)
{
    // Validate the user against the Membership framework user store
    if (Membership.ValidateUser(UserName.Text, Password.Text))
    {
        // Log the user into the site
        FormsAuthentication.SetAuthCookie(UserName.Text, true);
        // Do the redirect
        Response.Redirect("~/Index.aspx");
    }
    // If we reach here, the user's credentials were invalid
    InvalidCredentialsMessage.Visible = true;
}
protected void btnLoginButton_Click(object sender, EventArgs e)
{
    if (Membership.ValidateUser(UserName.Text, Password.Text))
    {
        WelcomeBackMessage.Text = User.Identity.Name.ToString();
        // Log the user into the site
        Response.Redirect("~/Index.aspx");

Place WelcomeBackMessage label in master page and try this code.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建帐户重定向后,MVC中的登录控件未显示用户名 - Login control in MVC not displaying username after account creation redirect 登录时显示全名而不是用户名 - Displaying Fullname On Login Rather Than Username 登录后如何显示用户名 - How to display the username after login 登录后如何在徽标下添加“用户名” - How To Add “Username” Under Logo After Login 填写用户名和密码后,当我按Enter键无法登录时,即google chrome工作正常。 怎么会? - after filling username and password when i press an enter button unable to login in ie but google chrome is working fine. how come? 登录失败后将用户名传递回MVC视图/文本框 - Passing username back to to MVC view/textbox after unsuccessful login 登录后如何在下一个表格的标签上显示欢迎用户名? - How to display welcome username in a label on next form after login? 登录asp.net后如何检索当前用户的用户名 - how to retrive the username of the current user after login in asp.net 登录表单后在标签上显示用户名和用户ID - Show username and user id on Label after Login Form 用户名和Passowrd文本框在登录后自动填充数据 - Username and Passowrd Textboxes automatically get's filled with the data after login
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM