C# 如何在登录后在默认页面上获取用户信息?

C# 如何在登录后在默认页面上获取用户信息?,c#,asp.net,login,C#,Asp.net,Login,我正在用asp制作我的第一个网页。 在我的项目中,我得到了不同的页面。 一个用于登录: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="User_Login_CS.Login" %> <!DOCTYPE html> <html> <head id="Head1" runat="server"> <title&

我正在用asp制作我的第一个网页。 在我的项目中,我得到了不同的页面。 一个用于登录:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="User_Login_CS.Login" %>

<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
    <title></title>
    <link href="Menustyle.css" rel="stylesheet" />
</head>
<body>
    <!--Menu -->
    <div>
        <nav>
            <ul>
                <li class="sub">
                    <a href="Index.aspx">
                        <img src="logo.png"></a>
                </li>
                <li>
                    <a href="DetailView.aspx?call=104"><span class="auto-style1"><strong>Enter Data</strong></span></a>
                </li>
                <li>
                    <a href="Overview.aspx"><span class="auto-style1"><strong>Overview</strong></span></a>
                </li>
                <li>
                    <a href="ManagementReport.aspx"><span class="auto-style1"><strong>Management Report</strong></span></a>
                </li>
            </ul>
        </nav>
    </div>

    <form id="form1" runat="server">
        <table class="auto-style2">
            <tr>
                <td class="auto-style5"></td>
                <td class="auto-style6"></td>
                <td class="auto-style7"></td>
            </tr>
            <tr>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style4">
                    <asp:Login ID="Login1" runat="server" OnAuthenticate="ValidateUser">
                    </asp:Login>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style4">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Security;

namespace User_Login_CS
{
    public partial class Login : System.Web.UI.Page
    {
        private const string CONNECTION_STRING = "Server=MyServer;Database=MyDB;Trusted_Connection=true";

        protected void ValidateUser(object sender, EventArgs e)
        {
            int userId = 0;

            using (SqlConnection con = new SqlConnection(CONNECTION_STRING))
            {
                using (SqlCommand cmd = new SqlCommand("Validate_User"))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Username", Login1.UserName);
                    cmd.Parameters.AddWithValue("@Password", Login1.Password);
                    cmd.Connection = con;
                    con.Open();
                    userId = Convert.ToInt32(cmd.ExecuteScalar());
                    con.Close();
                }
                switch (userId)
                {
                    case -1:
                        Login1.FailureText = "Username and/or password is incorrect.";
                        break;
                    case -2:
                        Login1.FailureText = "Account has not been activated.";
                        break;
                    default:
                        FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);
                        break;
                }
            }
        }
    }
}
登录后,我将用户重新强制设置到默认页面。我现在的问题是:我必须做什么才能保证我所有页面的用户身份验证都是有效的,我如何访问默认页面上的用户数据

谢谢你的帮助和时间!
问候

在类中加载用户数据并将其存储在会话中


不要将整个用户信息存储到会话中,只需将用户名或用户ID存储到cookie中:

protected void ValidateUser(object sender, EventArgs e)
{
    //.................

    //If user logins successfully then authenticate username into cookie
    FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet);
}
然后从项目中的任何地方,您都可以获取当前日志用户的用户名,并从数据库中加载相关的用户信息,如下所示:

if (HttpContext.Current.User.Identity.IsAuthenticated)
{
    var currentUserName = HttpContext.Current.User.Identity.Name;

    //Load other user info here...
    var userInfo = LoadUserInfoByUsername(currentUserName);

    //Do something with user info....
}

假设Login1.UserName,Login1.rememberset都是字符串类型

试试这个:

switch (userId)
                {
                    case -1:
                        Login1.FailureText = "Username and/or password is incorrect.";
                        break;
                    case -2:
                        Login1.FailureText = "Account has not been activated.";
                        break;
                    default:
Session["UserName"]=Login1.UserName; Session["RememberMeSet"]=Login1.RememberMeSet;

FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);
                        break;
                }

如前所述,在其他解决方案中,您必须使用会话。这里有几个很好的教程。只要用谷歌搜索就可以了。例如,这个:。
希望有帮助

如果用户登录成功,则返回用户记录并将用户数据存储到会话或cookie中。使用此数据到任何地方后。请在asp.net中用谷歌搜索如何使用会话*