creating custom HttpContext.Current.User.Identity

D

dSchwartz

I've started getting into using forms authentication for asp.net apps
with c#. From what i understand so far (limited) I like the way
things work! I've got an application working right now where an email
address and password is checked from a database and I can check the
authenticated user's email address with
HttpContext.Current.User.Identity.Name. This all works fine!

I want to be able to do:
HttpContext.Current.User.(customIdentity?).EmailAddress
HttpContext.Current.User.(customIdentity?).UserID
HttpContext.Current.User.(customIdentity?).NickName

I'm just a bit confused about what i have to do after reading many
different posts and articles about this. It seems to me like i should
only have to create a custom class that extends IIdentity, but then to
use that don't i have to create a custom class that extends IPrincipal
also? and then it also seems i need a custom
FormsAuthenticationTicket class also???

I'm just looking for the simplest way to do this, which classes do i
have to create custom for this action?

Thanks for your time!
 
J

Joe Kaplan \(MVP - ADSI\)

I think you can use the same IPrincipal that Forms auth uses, but if for
some reason you can't, you can easily use the GenericPrincipal class with
your custom IIdentity implementation. Its constructor takes any type
implementing IIdentity.

It is also totally reasonable to derive from GenericPrincipal or
GenericIdentity (or most of the framework IIdentity or IPrincipal
implementations for that matter) if you want.

Joe K.
 
J

Josh

I've never used a custom Identity w/o a custom principal also but I would
think you could just assign the customidentity to the current principals
identity in the global AuthenticateRequest event and then just access
anywhere you'd like after that?

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{

customIdentity ci;

if (Request.IsAuthenticated == true)
{
//load up the custom identity info based on the default
username found in name usually
//or based on a client cookie with the user id or something
ci = new
customIdentity(HttpContext.Current.User.Identity.Name);

//Assign the current identity to the newly loaded
customIdentity
HttpContext.Current.User.Identity = ci;

//or alternatlively (which I think is the same thing as
above)
System.Threading.Thread.CurrentPrincipal.Identity = ci;

}
}

Now whenever you wanted to get at info in your custom identity you'd just
need to do something like this:

(customIdentity) HttpContext.Current.User.Identity.NickName
or
(customIdentity) HttpContext.Current.User.Identity.EmailAddress

Forgive my poor c# skills, I'm a VB programmer making the transition to c#
:).

Although I've never done a custom identity w/o doing a custom principal I
can't see why this wouldn't work.

Josh
 
J

Joe Kaplan \(MVP - ADSI\)

Agreed. It is a nice addition to the Application Blocks collection and uses
good patterns.

Joe K.
 
D

dSchwartz

Josh said:
I've never used a custom Identity w/o a custom principal also but I would
think you could just assign the customidentity to the current principals
identity in the global AuthenticateRequest event and then just access
anywhere you'd like after that?


I'm very close to making this work. Here's what I've got:


protected void Application_AuthenticateRequest(Object sender,
EventArgs e)
{ string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
if (null == authCookie)
{ //There is no authentication cookie.
return;
}

FormsAuthenticationTicket authTicket = null;
try
{ authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch(Exception ex)
{ //Log exception details (omitted)
Response.Write("execption:" + ex);
return;
}

if (null == authTicket)
{ //cookie failed to decrypt
return;
}

string[] roles = authTicket.UserData.Split(new char[] {'|'});

inetIdentity i1;
i1 = new inetIdentity(HttpContext.Current.User.Identity.Name);

GenericPrincipal principal = new GenericPrincipal(i1, roles);

Context.User = principal;
}

My inetIdentity which extends IIdentity has just the 2 added
properties Userid and EmailAddress. It's constructor looks up the
Nickname and userID from the db based on the emailaddress and assigns
those values. That all works good!

To get these new values I do:
((inetIdentity)HttpContext.Current.User.Identity).EmailAddress
which works good when I've got an authenticated user. When there is
no authenticated user I get "System.InvalidCastException: Specified
cast is not valid."

I assume I'm just a little bit off here but not exactly sure where.
someone please point me in the right direction here. Thanks!
 
N

.NET Follower

hi,
((inetIdentity)HttpContext.Current.User.Identity).EmailAddress
at this point u dont hv the cokie with u
so i tkink u shud add

If (Request.IsAuthenticated)
{
((inetIdentity)HttpContext.Current.User.Identity).EmailAddress
}



--
Thanks and Regards,

Amit Agarwal
Software Programmer(.NET)
dSchwartz said:
"Josh" <[email protected]> wrote in message
I've never used a custom Identity w/o a custom principal also but I would
think you could just assign the customidentity to the current principals
identity in the global AuthenticateRequest event and then just access
anywhere you'd like after that?


I'm very close to making this work. Here's what I've got:


protected void Application_AuthenticateRequest(Object sender,
EventArgs e)
{ string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
if (null == authCookie)
{ //There is no authentication cookie.
return;
}

FormsAuthenticationTicket authTicket = null;
try
{ authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch(Exception ex)
{ //Log exception details (omitted)
Response.Write("execption:" + ex);
return;
}

if (null == authTicket)
{ //cookie failed to decrypt
return;
}

string[] roles = authTicket.UserData.Split(new char[] {'|'});

inetIdentity i1;
i1 = new inetIdentity(HttpContext.Current.User.Identity.Name);

GenericPrincipal principal = new GenericPrincipal(i1, roles);

Context.User = principal;
}

My inetIdentity which extends IIdentity has just the 2 added
properties Userid and EmailAddress. It's constructor looks up the
Nickname and userID from the db based on the emailaddress and assigns
those values. That all works good!

To get these new values I do:
((inetIdentity)HttpContext.Current.User.Identity).EmailAddress
which works good when I've got an authenticated user. When there is
no authenticated user I get "System.InvalidCastException: Specified
cast is not valid."

I assume I'm just a little bit off here but not exactly sure where.
someone please point me in the right direction here. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top