Display user Email and last login date after login using C# in ASP.Net

kankon
 
on Sep 14, 2021 10:48 PM
666 Views

i use in welocme page can i add more details ?

Simple User Login Form example in ASP.Net

<div>
    Welcome
    <asp:LoginName ID="LoginName1" runat="server" Font-Bold = "true" />
    <br />
    <br />
    <asp:LoginStatus ID="LoginStatus1" runat="server" />
</div>

i would like add also under name email and lastlogindate

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 15, 2021 04:57 AM

Hi kankon,

Refer below code.

HTML

Home

<div>
    Welcome<asp:LoginName ID="LoginName1" runat="server" Font-Bold="true" />
    <br /><br />
    <asp:Label ID="lblEmail" runat="server" />
    <asp:Label ID="lblLastLoginDate" runat="server" />
    <asp:LoginStatus ID="LoginStatus1" runat="server" />
</div>

Code

Home

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.Page.User.Identity.IsAuthenticated)
    {
        FormsAuthentication.RedirectToLoginPage();
    }
    else
    {
        string name = this.Page.User.Identity.Name;
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE Username = @Name"))
            {
                cmd.Parameters.AddWithValue("@Name", name);
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        lblLastLoginDate.Text = dt.Rows[0]["LastLoginDate"].ToString();
                        lblEmail.Text = dt.Rows[0]["Email"].ToString();
                    }
                }
            }
        }
    }
}

Rest code will remain same as in the sample.

Simple User Login Form example in ASP.Net