Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

how to get username in login control using asp.net membership

Jan 6, 2011
Introduction

Here I will explain how can we get username and userid of currently logged user in asp.net membership.

Description

I am working on application by using login control in asp.net at that time I got requirement like getting username and user id of logged in user for that I have written code like this to get logged in user username and userid in asp.net membership.


MembershipUser currentUser = Membership.GetUser();
//Get Username of Currently logged in user
string username = currentUser.UserName;
//Get UserId of Currently logged in user
string UserId = currentUser.ProviderUserKey.ToString();

By using above code we can get currently logged user in login control 

Here i written much code like from Membership i am getting currently logged in username  no need to use MembershipUser event to get currently logged in user we have another simple way to get currently logged in username you just define like this in your page
 
string userName = Page.User.Identity.Name;

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

22 comments :

Ajay said...

Hi,

Thanks Really Useful information.

This code helps me a lot- "Page.User.Identity.Name;"

It working perfect. Now i can display the current user name on Home page. But i got a small issue....It display whole user email id on home page like Welcome xyz@gmail.com BUt i want to display only XYZ on home page is it possible using this logic? If so, please reply....

Thank you...

-Ajay

Suresh Dasari said...

@Ajay..
Split your username with @ operator like this

string strData = "suresh@gmail.com";
char[] separator = new char[] { '@' };
string[] strSplitArr = strData.Split(separator);
string username=strSplitArr[0]

After split your username as shown above you will get username

Ajay said...

Thanks

Anonymous said...

Dear Suresh,

Your Blog is very helpful, God Bless.

I request you to please share how we can extract image file from database into crystal report.

Also write detail blog on crystal reports designing and functionality

Thank you

Imtiaz

Anonymous said...

if the user forget his password then after submitting his email address how the email address authenticated with database and after authentication sends the login details in the email

Anonymous said...

Do one thing for any user if forgot password ..i am givinu u code...first store the user name or password on database then if any user may forget password then the password will send him via email id.....this code definitely work try it....
...........................
C# code....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnPass_Click(object sender, EventArgs e)

{

//Create Connection String And SQL Statement

//string strConnection = ConfigurationManager.ConnectionStrings["goodwillConnectionString"].ConnectionString;


string strSelect = "SELECT UserName,Password FROM Users WHERE Email = @Email";



SqlConnection connection = new SqlConnection("Data Source=ABHISHEK-PC\\SA;Integrated Security=true;Initial Catalog=goodwill");
connection.Open();
SqlCommand command = new SqlCommand();

command.Connection = connection;

command.CommandType = CommandType.Text;

command.CommandText = strSelect;
SqlParameter email = new SqlParameter("@Email", SqlDbType.VarChar, 50);

email.Value = txtEmail.Text.Trim().ToString();

command.Parameters.Add(email);
//Create Dataset to store results and DataAdapter to fill Dataset

DataSet dsPwd = new DataSet();

SqlDataAdapter dAdapter = new SqlDataAdapter(command);



dAdapter.Fill(dsPwd);

connection.Close();

if(dsPwd.Tables[0].Rows.Count > 0 )

{

MailMessage loginInfo = new MailMessage();

loginInfo.To.Add(txtEmail.Text.ToString());

loginInfo.From = new MailAddress("id@gmail.com");

loginInfo.Subject = "Forgot Password Information";



loginInfo.Body = "Username: " + dsPwd.Tables[0].Rows[0]["UserName"] + "

Password: " + dsPwd.Tables[0].Rows[0]["Password"] + "

";
loginInfo.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient();

smtp.Host = "smtp.gmail.com";

smtp.Port = 587;

smtp.EnableSsl = true;

smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "pwd");

smtp.Send(loginInfo);
lblMessage.Text = "Password is sent to you email id,you can now ";

}

else

{

lblMessage.Text = "Email Address Not Registered";

}



}

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

Hi, Can you please suggest me ASP-c# snippet to get list of users logged in currently?

Unknown said...

Hai Mr.Suresh...
After Successfully Login,how to get the logined username into home page lable.

Anonymous said...

Thanx man u help me a lot in every a problem i face...!

Anonymous said...

Hi Mr. Suresh...
I am a newbie to asp.net techonology. I have a query that how can we diplay user picture to image control (image control is on master page) on which he had uploaded during registration on the users successful login???
Thank-u in advance!!!





Unknown said...

Prevent Simultaneous Logins by a Single User ID ... How to do that

Unknown said...

Hi Suresh annay..I love your articles very much ..those are helping me alot..I need a google login with userdetails in my website.so pls forward me as soon as possible .
Thank you soo much annay..

Unknown said...

Hi Suresh,

Im not getting username from Membership.GetUser() . Kindly give complete source code.
Thanks for sharing your knowledge with us. Your complete series of articles on .net, c# are very helpful for me . For my company's any project I prefer to refer your articles when needed. Keep sharing

Anonymous said...

thank you so much sir

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.