登录后无法显示用户名 [英] Displaying the username after login not working

查看:96
本文介绍了登录后无法显示用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

登录索引页后,显示用户名时遇到麻烦.

I am having trouble displaying the username after login on my index page.

我用于登录的代码:

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

然后在我的索引页中:

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

后面的代码:

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.

谢谢

推荐答案

您只是忘记了实际登录用户.您要做的只是验证用户凭据,然后进行重定向.因此,请尝试在if语句中添加以下行:

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:

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

这篇关于登录后无法显示用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆