简体   繁体   English

如何在主页中使用当前用户登录

[英]how to do Log IN with current User in the masterpage

how can i do a login with current user? 我该如何登录当前用户? the current user will be displayed in the masterpage of the default homepage and the label from the masterpage will inherit to the content pages of the master page. 当前用户将显示在默认主页的主页中,并且该主页中的标签将继承到该主页的内容页面。

here is my login page asp code: 这是我的登录页面的ASP代码:

<div class="container-fluid">

<form class="form-signin" runat="server">
    <h1 class="form-signin-heading text-muted">Sign In</h1>
    <asp:TextBox ID ="email" runat="server" CssClass="form-control" placeholder="Email Address"></asp:TextBox>
    <asp:TextBox ID ="password" runat="server" CssClass="form-control" placeholder="Password" TextMode="Password"></asp:TextBox>
    <br />

    <asp:Button ID="btnLogIN" runat="server" CssClass="btn btn-primary btn-block" Text="Log In" OnClick="btnLogIN_Click" />

</form>

and my aspx.cs code is here and i dont know if this is correct. 我的aspx.cs代码在这里,我不知道这是否正确。

protected void btnLogIN_Click(object sender, EventArgs e)
    {
        Utility u = new Utility();
        string conn = u.connect();
        SqlConnection connUser = new SqlConnection(conn);
        SqlCommand read = connUser.CreateCommand();
        SqlDataReader reader = null;

        int empid = 0;
        string dbuser = "";
        string dbpword = "";

        string username = email.Text;
        string passwords = password.Text;

        string login = "Select * from MOSEFAccount where UserName = '" + username + "' AND Password = '" + passwords + "'";

        try
        {
            connUser.Open();
            read.CommandText = login;
            reader = read.ExecuteReader();
        }
        catch
        {
            Console.WriteLine("Error");
        }
        while (reader.Read())
        {
            empid = reader.GetInt32(0);
            dbuser = reader.GetString(1);
            dbpword = reader.GetString(2);
        }

        if (username == dbuser && passwords == dbpword)
        {

            Response.Redirect("~/Default.aspx?ID=" + empid);
        }
        else
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(@"<script type ='text/javascript'>");
            sb.Append("alert('Invalid Account');");
            sb.Append(@"</script>");
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "EditHideModalScript", sb.ToString(), false);
        }
        connUser.Close();
    }

Can you elaborate on the meaning of current user? 您能否详细说明当前用户的含义? Unless you login you are not logged in user but anonymous user. 除非您登录,否则您不是登录用户,而是匿名用户。

Update: 更新:

Store the pulled data in Session object and access it anytime whenever and wherever you like. 将提取的数据存储在Session对象中,并随时随地访问。 For example: For storing details use:- 例如:要存储详细信息,请使用:-

if (username == dbuser && passwords == dbpword)
{
    Session["UserName"] = username;
    Session["EmpId"] = empid;
    Response.Redirect("~/Default.aspx?ID=" + empid);
}

For displaying use (in the master page): 用于显示用途(在母版页中):

<%=Session["UserName"]%>
<%=Session["EmpId"]%>

You can build upon this like creating a User class and then creating an instance of this class and storing the instance itself in Session. 您可以以此为基础,例如创建User类,然后创建该类的实例并将该实例本身存储在Session中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM