Link to home
Start Free TrialLog in
Avatar of frosty1
frosty1

asked on

How to get userid directly after login in

I'm wonder how i can get the userid just after i validate. Is there another method i can use to login than  

 FormsAuthentication.RedirectFromLoginPage(userName, true);  



  if (Membership.ValidateUser(userName, password))
        {

// i get a null object on the below as i haven't logged in yet
           
 Guid UserID = new Guid(Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString());

// some other logic that requires UserId

 
        }
Avatar of WesWilson
WesWilson
Flag of United States of America image

I assume you are using the Login server control? In the example below it is named "LoginControl."

protected void LoginControl_LoggingIn(object sender, LoginCancelEventArgs e)
{
    if (Membership.ValidateUser(LoginControl.UserName, password))
    {
        Guid UserID = new Guid(Membership.GetUser(LoginControl.UserName).ProviderUserKey.ToString());
        //...
    }
}

Open in new window

Using a login screen on VB.NET, if the login is successful I pass it to a shared module variable on the resulting form, so I can use the current user logged in to display in a status strip.
ASKER CERTIFIED SOLUTION
Avatar of Kumaraswamy R
Kumaraswamy R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Frosty1, would you mind explaining why you accepted rkworlds' solution without a point split? Was there a problem with my solution? Is it that you prefer the LoggedIn event rather than the LoggingIn event which fires earlier?

I think your problem was that User.Identity.Name threw a null reference exception. Rkworlds and I solved that the same way.

We both retrieved the userID the same way. I kept your logic to create a new GUID as you didn't explain what you wanted to do with the userID once you got it.

Rkworlds has a fine solution and I know it is too late to split points, but for my future reference I would like to know how to make my solutions more relevant or explain them better, so I'm curious why his solution was preferred.

Avatar of frosty1
frosty1

ASKER

Sorry wes, seemed to have missed that response.  you were spot on.  Apologies :)
No problem. Don't worry about it. Have a good day!