简体   繁体   中英

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:

<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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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