Answered by:
Get the UserID of the current logged in user

Question
-
User-1506996564 posted
Hello
I use the following code to get the current user
Protected Sub LoginUser_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginUser.LoggedIn Dim currentUser As MembershipUser = Membership.GetUser(User.Identity.Name) Dim username As String = currentUser.UserName Dim userID As Guid = currentUser.ProviderUserKey ''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim sql As String = "SELECT CustomerID,CustomerName FROM Customer WHERE UserID = " + userID.ToString Dim myConnection As SqlConnection = New SqlConnection("Data Source=mssql501.ixwebhosting.com;Initial Catalog=C209256_library;User ID=C209256_library; Password=A123456789") Dim myCommand As SqlCommand = New SqlCommand(sql, myConnection) myConnection.Open() Dim myReader As SqlDataReader = myCommand.ExecuteReader() If myReader.Read() Then Session.Add("CustomerID", myReader(0)) Session.Add("CustomerName", myReader(1)) End If myReader.Close() myConnection.Close() '''''''''''''''''''''''''''''''''''''''''''''''''''''' End Sub
but I get the following error
Object reference not set to an instance of an object.
any ideas please?
thanks
Saturday, May 3, 2014 6:11 AM
Answers
-
User-1199946673 posted
When using the Login Control, the user will be logged in AFTER the first request. In the LoggedIn evente, User.Identity.Name will not be availlable. Instead, use LoginUser.Username
Dim currentUser As MembershipUser = Membership.GetUser(LoginUser.UserName)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 3, 2014 6:18 AM
All replies
-
User-1199946673 posted
When using the Login Control, the user will be logged in AFTER the first request. In the LoggedIn evente, User.Identity.Name will not be availlable. Instead, use LoginUser.Username
Dim currentUser As MembershipUser = Membership.GetUser(LoginUser.UserName)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 3, 2014 6:18 AM -
User-1506996564 posted
That was great
thank you
Saturday, May 3, 2014 6:22 AM -
User1508394307 posted
Not clear where exactly you got that error but it seems that User.Identity.Name is null at this moment of time. The LoggedIn event is raised after the authentication provider checks the user's credentials and the authentication cookie is queued to send to the browser in the next response. This probably means that User object is null at this method. Read more here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.loggedin(v=vs.110).aspx
There are many posts regarding same issue where it is recommended to use either LoginUser.UserName or Membership.GetUser(LoginUser.UserName, true);
http://forums.asp.net/t/1663240.aspx?User+Identity+Name+Is+it+accessible+from+LoginUser_LoggedIn+or
System.Web.UI.WebControls.Login controlLogin = (Login)sender;
MembershipUser user = (MembershipUser)Membership.GetUser(controlLogin.UserName);
http://stackoverflow.com/questions/12448801/asp-net-membership-login-redirect-user-after-logged-inSaturday, May 3, 2014 6:41 AM