Asp.net logged in another membership provider get null error

jtuttle99
jtuttle99 used Ask the Experts™
on
If I log in different membership provider, and go to page that contains following code, I get NullReferenceException error. User will be authenticated even if I loggin to different membership.
Is there any ways to prevent from getting error??


If User.Identity.IsAuthenticated Then
  Dim UserIdentity As MembershipUser = Membership.Providers("CustMembershipProvider").GetUser(HttpContext.Current.User.Identity.Name, False)
  Dim currentUserID As String = UserIdentity.ProviderUserKey.ToString()

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
HI

If ((User.Identity.IsAuthenticated ) and  ( HttpContext.Current.User.Identity.Name = nothing) )Then


  Dim UserIdentity As MembershipUser = Membership.Providers("CustMembershipProvider").GetUser(HttpContext.Current.User.Identity.Name, False)
  Dim currentUserID As String = UserIdentity.ProviderUserKey.ToString()

http://forums.asp.net/p/1515236/3623214.aspx

Author

Commented:

The code works, but when I logged in correct membership provider, I'm also kicked out of the page.
There are two things that I want to achieve.

1. check if user is authenticated or not
2. if so, check user is logged in correct membership provider

Are there any ways to check if I logged in right membership provider?

Thank you for your help!


Author

Commented:
Here is my situation. I have membership providers for employee(for maintain contents) and customer.

Customer can't go to employee page, because admin side is protected by roles.

Log in page is only allowed to log in employee log in.

So, the problem is that if employee goes to some customer's side of the web site, this null error happens.

Employee status seem to be authenticated (by employee membership provider), so it passes "If User.Identity.IsAuthenticated then"

 

I would like to know how to escape these codes if user is logged in to employee membership provider.

 

something like this...

If userlogged in employee membership provider then...

 

Anyone??
Most Valuable Expert 2012
Top Expert 2014
Commented:
I think you can use a Session variable. When the employee is authenticated, add a boolean flag to the session called IsEmp. Then in customer pages, check if IsEmp exists and if its true then ignore the code.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial