views:

36

answers:

3

Hi,

We have got two distinct lists of users that we need to power logged in access to sections of our site. These lists can't be combined, as one is synced daily to an externally hosted data source, and both tables have to be 100% the same.

We have set up two Membership providers onto the site, but my question is, is it possible to allow both to be logged in at the same time?

The issue I find is that HttpContext.Current.User.identity.name contains the username of the last successful logon.

A: 

would the suggestion outlined here be of any help?

http://stackoverflow.com/questions/2658294/can-siteb-restrict-access-only-to-users-authenticated-on-site-a-how

DaveDev
A: 

Use some sort of delimiter and put both users into the the login Identity. e.g. mp3duck/employee1

When you login in via membership provider 1, you would do something like

// extract the existing prov2ID so you can keep it. GetID() would be a string split function
string prov2ID = GetID(User.Identity.Name, 2); 
FormsAuthentication.SetAuthCookie(prov1ID + "/" + prov2ID, false);

And visa versa for a login via membership provider 2.

If you are using the <asp:login> control, you would modify the UserName propery instead of calling SetAuthCookie() since the login control calls SetAuthCookie() itself.

You can put any sort of stuff into the Identity name. I often use it to store both a number int ID for the user primary key, and their username. The only downside is you have to clean it up when displaying it in the html. e.g. Hello <% User.Identity.Name %> would display a lot more than you wanted.

Jacob
Thanks for the reply Jacob. I've added a follow up question below
mp3duck
A: 

Thanks for the reply Jacob. That is the sort of thing I am looking for.

However, can you let me know where I need to place this code?

I tried putting this into the _Authenticate method, but it doesn't work. (User.Identity.Name only contains the current username, and not what I appended)

        if (Membership.Providers["MyProvider"].ValidateUser(Login1.UserName, Login1.Password))
        {

            e.Authenticated = true;
            FormsAuthentication.SetAuthCookie(Login1.UserName + "_M", false);

        }
mp3duck
Right, I had it in the right place, but as I was also setting e.Authenticated = true, and not redirecting myself, it went on to reset the cookie itself.I got some follow up code from here:http://msdn.microsoft.com/en-us/library/system.web.security.formsauthenticationticket.aspx
mp3duck
The <asp:login> control does the SetAuthCookie() call for you. In which case you can just modify Login1.UserName. e.g. e.Authenticated = true; Login1.UserName = Login1.UserName + "_M";
Jacob