[英]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.