HighTechTalks DotNet Forums  

Override User.Identity.Name or Custom IIdentity

ASP.net Security microsoft.public.dotnet.framework.aspnet.security


Discuss Override User.Identity.Name or Custom IIdentity in the ASP.net Security forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Corker
 
Posts: n/a

Default Override User.Identity.Name or Custom IIdentity - 03-08-2010 , 05:32 PM






Hi I am using the standard asp.net login control and it is currently working
well where my users can login with their email address and password.
I want to modify this so they can enter their email address or membership
number and password. I have a custom membership provider class where I check
the login details based on whether an email or number has been entered, again
this is fine.
Finally, my problem is that User.Identity.Name is whatever was entered in
the login box, I want this to always be an email address so can you suggest
how I can override this value?
I created a Custom Identity class and tried assigning that in Global.asax
Application_AuthenticateRequest, but it says it cannot resolve the type
MyNamespace.CustomIdentity. I'm not sure what to try next.

Reply With Quote
  #2  
Old   
Joe Kaplan
 
Posts: n/a

Default Re: Override User.Identity.Name or Custom IIdentity - 03-09-2010 , 06:31 PM






You are close to getting this. The authenticate event on the ASP.NET
pipeline is the proper event to handle. Basically, you want your custom code
to run after the various authentication mechanisms like forms auth and
membership but before authorization takes place. Essentially, you want to be
the last handler of the Authenticate event. Here, you can swap out the
principal for whatever you want.

Your issue sounds like a simple problem related to type references where the
..NET framework cannot find your class by the name you tried to use for it.
This is a more generic issue that you should be able to figure out. Using
the object browser to find the real name of your type may be helpful.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
"Corker" <Corker (AT) discussions (DOT) microsoft.com> wrote

Quote:
Hi I am using the standard asp.net login control and it is currently
working
well where my users can login with their email address and password.
I want to modify this so they can enter their email address or membership
number and password. I have a custom membership provider class where I
check
the login details based on whether an email or number has been entered,
again
this is fine.
Finally, my problem is that User.Identity.Name is whatever was entered in
the login box, I want this to always be an email address so can you
suggest
how I can override this value?
I created a Custom Identity class and tried assigning that in Global.asax
Application_AuthenticateRequest, but it says it cannot resolve the type
MyNamespace.CustomIdentity. I'm not sure what to try next.

Reply With Quote
  #3  
Old   
Everardo Garcia
 
Posts: n/a

Default Re: Override User.Identity.Name or Custom IIdentity - 07-01-2011 , 09:11 AM



Hi

We had this problem a few days ago, you can set User value, however the authentication cookie is created with the text entered in the user name textbox, then the cookie is included in the response to the browser and when the browser made a request the cookie is decrypted and the original user name is assigned to the Identity (User).

Assign the new user name on login_Authenticate event when the user succesfully authenticate:

GenericIdentity identity = new GenericIdentity(yourNewUserName, "Forms");
HttpContext.Current.User = new GenericPrincipal(identity, new string[]{});
System.Threading.Thread.CurrentPrincipal = HttpContext.Current.User;


You can watch cookies in debug mode using Response.Cookies, but there are no cookie in Authenticate event, then you use login_LoggedIn event to clear cookie collection and set a new cookie with your new user name:

protected void login_LoggedIn(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(User.Identity.Name))
{
Response.Cookies.Clear();
FormsAuthentication.SetAuthCookie(User.Identity.Na me, this.lgnLogin.RememberMeSet);
}
}

Good luck!

Quote:
On Monday, March 08, 2010 6:32 PM Corker wrote:

Hi I am using the standard asp.net login control and it is currently working
well where my users can login with their email address and password.
I want to modify this so they can enter their email address or membership
number and password. I have a custom membership provider class where I check
the login details based on whether an email or number has been entered, again
this is fine.
Finally, my problem is that User.Identity.Name is whatever was entered in
the login box, I want this to always be an email address so can you suggest
how I can override this value?
I created a Custom Identity class and tried assigning that in Global.asax
Application_AuthenticateRequest, but it says it cannot resolve the type
MyNamespace.CustomIdentity. I am not sure what to try next.

Quote:
On Tuesday, March 09, 2010 7:31 PM Joe Kaplan wrote:

You are close to getting this. The authenticate event on the ASP.NET
pipeline is the proper event to handle. Basically, you want your custom code
to run after the various authentication mechanisms like forms auth and
membership but before authorization takes place. Essentially, you want to be
the last handler of the Authenticate event. Here, you can swap out the
principal for whatever you want.

Your issue sounds like a simple problem related to type references where the
.NET framework cannot find your class by the name you tried to use for it.
This is a more generic issue that you should be able to figure out. Using
the object browser to find the real name of your type may be helpful.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net

Reply With Quote
  #4  
Old   
Everardo Garcia
 
Posts: n/a

Default Re: Override User.Identity.Name or Custom IIdentity - 07-01-2011 , 09:12 AM



Hi

We had this problem a few days ago, you can set User value, however the authentication cookie is created with the text entered in the user name textbox, then the cookie is included in the response to the browser and when the browser made a request the cookie is decrypted and the original user name is assigned to the Identity (User).

Assign the new user name on login_Authenticate event when the user succesfully authenticate:

GenericIdentity identity = new GenericIdentity(yourNewUserName, "Forms");
HttpContext.Current.User = new GenericPrincipal(identity, new string[]{});
System.Threading.Thread.CurrentPrincipal = HttpContext.Current.User;


You can watch cookies in debug mode using Response.Cookies, but there are no cookie in Authenticate event, then you use login_LoggedIn event to clear cookie collection and set a new cookie with your new user name:

protected void login_LoggedIn(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(User.Identity.Name))
{
Response.Cookies.Clear();
FormsAuthentication.SetAuthCookie(User.Identity.Na me, this.lgnLogin.RememberMeSet);
}
}

Good luck!

Quote:
On Monday, March 08, 2010 6:32 PM Corker wrote:

Hi I am using the standard asp.net login control and it is currently working
well where my users can login with their email address and password.
I want to modify this so they can enter their email address or membership
number and password. I have a custom membership provider class where I check
the login details based on whether an email or number has been entered, again
this is fine.
Finally, my problem is that User.Identity.Name is whatever was entered in
the login box, I want this to always be an email address so can you suggest
how I can override this value?
I created a Custom Identity class and tried assigning that in Global.asax
Application_AuthenticateRequest, but it says it cannot resolve the type
MyNamespace.CustomIdentity. I am not sure what to try next.

Quote:
On Tuesday, March 09, 2010 7:31 PM Joe Kaplan wrote:

You are close to getting this. The authenticate event on the ASP.NET
pipeline is the proper event to handle. Basically, you want your custom code
to run after the various authentication mechanisms like forms auth and
membership but before authorization takes place. Essentially, you want to be
the last handler of the Authenticate event. Here, you can swap out the
principal for whatever you want.

Your issue sounds like a simple problem related to type references where the
.NET framework cannot find your class by the name you tried to use for it.
This is a more generic issue that you should be able to figure out. Using
the object browser to find the real name of your type may be helpful.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net

Quote:
On Friday, July 01, 2011 10:10 AM Everardo Garcia wrote:

Hi



We had this problem a few days ago, you can set User value, however the authentication cookie is created with the text entered in the user name textbox, then the cookie is included in the response to the browser and when the browser made a request the cookie is decrypted and the original user name is assigned to the Identity (User).



Assign the new user name on login_Authenticate event when the user succesfully authenticate:



GenericIdentity identity = new GenericIdentity(yourNewUserName, "Forms");

HttpContext.Current.User = new GenericPrincipal(identity, new string[]{});

System.Threading.Thread.CurrentPrincipal = HttpContext.Current.User;





You can watch cookies in debug mode using Response.Cookies, but there are no cookie in Authenticate event, then you use login_LoggedIn event to clear cookie collection and set a new cookie with your new user name:



protected void login_LoggedIn(object sender, EventArgs e)

{

if (!string.IsNullOrWhiteSpace(User.Identity.Name))

{

Response.Cookies.Clear();

FormsAuthentication.SetAuthCookie(User.Identity.Na me, this.lgnLogin.RememberMeSet);

}

}



Good luck!

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.