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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
Kumaraswamy RCommented:


protected void Logintest_LoggedIn(object sender, System.EventArgs e)
        string MemberID;
        MemberID = Membership.GetUser(Logintest.UserName).ProviderUserKey.ToString();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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.

frosty1Author Commented:
Sorry wes, seemed to have missed that response.  you were spot on.  Apologies :)
No problem. Don't worry about it. Have a good day!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.