登录后无法显示用户名

编程入门 行业动态 更新时间:2024-10-28 07:28:57
本文介绍了登录后无法显示用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

登录后无法显示用户名

本文发布于:2023-10-24 03:05:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522759.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户名

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!