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
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. |
|||
|
|||
22 comments :
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
@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
Thanks
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
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
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";
}
}
Hi, Can you please suggest me ASP-c# snippet to get list of users logged in currently?
Hai Mr.Suresh...
After Successfully Login,how to get the logined username into home page lable.
Thanx man u help me a lot in every a problem i face...!
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!!!
Prevent Simultaneous Logins by a Single User ID ... How to do that
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..
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
thank you so much sir
Note: Only a member of this blog may post a comment.