Ask a Question related to ASP.NET Security, Design and Development.
-
dSchwartz #1
creating custom HttpContext.Current.User.Identity
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?).EmailAd dress
HttpContext.Current.User.(customIdentity?).UserID
HttpContext.Current.User.(customIdentity?).NickNam e
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!
dSchwartz Guest
-
flow user identity to web service through httpcontext??
hi all is there anyway to get access to the httpcontext of a web service call so that a custom principal object can be carried in the... -
HttpContext.Current.User.IsInRole
Hi, I have a problem when I am using the HttpContext.Current.User.IsInRole... This is my code: if (HttpContext.Current.User.IsInRole("Admin... -
How secure is HttpContext.Current.User.Identity.Name ?
How secure it is to authorize access to an ASP.NET application based on the value of the HttpContext.Current.User.Identity.Name propery? I... -
Web.HttpContext.Current.User.Identity.Name is blank
I am using an application which is a modification of IBuySpy Portal. It is using Forms authentication. Users login and their name is added to... -
HttpContext.Current.User not available in the redirected page
I am trying to use Forms Authentication and Role-Based Security: I have two pages: login.aspx and Default.aspx. -------------------------- This is... -
Joe Kaplan \(MVP - ADSI\) #2
Re: creating custom HttpContext.Current.User.Identity
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.
"dSchwartz" <schwartz@cableone.net> wrote in message
news:4ae1ece2.0402181442.70de4e41@posting.google.c om...> 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?).EmailAd dress
> HttpContext.Current.User.(customIdentity?).UserID
> HttpContext.Current.User.(customIdentity?).NickNam e
>
> 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!
Joe Kaplan \(MVP - ADSI\) Guest
-
Josh #3
Re: creating custom HttpContext.Current.User.Identity
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.N ame);
//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
"Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote
in message news:e%23FoUfq9DHA.1592@TK2MSFTNGP10.phx.gbl...> 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.
>
> "dSchwartz" <schwartz@cableone.net> wrote in message
> news:4ae1ece2.0402181442.70de4e41@posting.google.c om...>> > 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?).EmailAd dress
> > HttpContext.Current.User.(customIdentity?).UserID
> > HttpContext.Current.User.(customIdentity?).NickNam e
> >
> > 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!
>
Josh Guest
-
Hernan de Lahitte #4
Re: creating custom HttpContext.Current.User.Identity
This might be a good scenario to use the Authorization & Profiling
Application Block
([url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html[/url]
/authpro.asp)
Here you have the concept of an Extended Principal and this enables you to
build you custom Profile Provider (as well as an Autorization Provider) with
all the attributes you might need.
HernanDL.
"Josh" <jcarlisle@nospam-removeme.viewfusion.com> wrote in message
news:Ov0q8tq9DHA.1936@TK2MSFTNGP12.phx.gbl...something> 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 orwith> ci = new
> customIdentity(HttpContext.Current.User.Identity.N ame);
>
> //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
>
>
> "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote
> in message news:e%23FoUfq9DHA.1592@TK2MSFTNGP10.phx.gbl...> > 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>> > 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.
> >
> > "dSchwartz" <schwartz@cableone.net> wrote in message
> > news:4ae1ece2.0402181442.70de4e41@posting.google.c om...> >> > > 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?).EmailAd dress
> > > HttpContext.Current.User.(customIdentity?).UserID
> > > HttpContext.Current.User.(customIdentity?).NickNam e
> > >
> > > 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!
> >
>
Hernan de Lahitte Guest
-
Joe Kaplan \(MVP - ADSI\) #5
Re: creating custom HttpContext.Current.User.Identity
Agreed. It is a nice addition to the Application Blocks collection and uses
good patterns.
Joe K.
"Hernan de Lahitte" <hernan@lagash.com> wrote in message
news:%23jrD9Fu9DHA.2368@TK2MSFTNGP11.phx.gbl...([url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html[/url]> This might be a good scenario to use the Authorization & Profiling
> Application Block
>with> /authpro.asp)
> Here you have the concept of an Extended Principal and this enables you to
> build you custom Profile Provider (as well as an Autorization Provider)would> all the attributes you might need.
>
> HernanDL.
>
>
> "Josh" <jcarlisle@nospam-removeme.viewfusion.com> wrote in message
> news:Ov0q8tq9DHA.1936@TK2MSFTNGP12.phx.gbl...> > I've never used a custom Identity w/o a custom principal also but Ie)> > 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, EventArgsjust> something> > {
> >
> > 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> > ci = new
> > customIdentity(HttpContext.Current.User.Identity.N ame);
> >
> > //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'dc#> > 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 toI> > :).
> >
> > Although I've never done a custom identity w/o doing a custom principalwrote> > can't see why this wouldn't work.
> >
> > Josh
> >
> >
> > "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com>for> > in message news:e%23FoUfq9DHA.1592@TK2MSFTNGP10.phx.gbl...> > > I think you can use the same IPrincipal that Forms auth uses, but ifapps> with> > > some reason you can't, you can easily use the GenericPrincipal class> > > 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.
> > >
> > > "dSchwartz" <schwartz@cableone.net> wrote in message
> > > news:4ae1ece2.0402181442.70de4e41@posting.google.c om...
> > > > I've started getting into using forms authentication for asp.net> > > > with c#. From what i understand so far (limited) I like the way
> > > > things work! I've got an application working right now where anshould> > > > 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?).EmailAd dress
> > > > HttpContext.Current.User.(customIdentity?).UserID
> > > > HttpContext.Current.User.(customIdentity?).NickNam e
> > > >
> > > > 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 ito> > > > only have to create a custom class that extends IIdentity, but thenIPrincipal> > > > use that don't i have to create a custom class that extends>> >> > > > 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!
> > >
> > >
> >
>
Joe Kaplan \(MVP - ADSI\) Guest
-
dSchwartz #6
Re: creating custom HttpContext.Current.User.Identity
"Josh" <jcarlisle@nospam-removeme.viewfusion.com> wrote in message news:<Ov0q8tq9DHA.1936@TK2MSFTNGP12.phx.gbl>...
> 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.Nam e);
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!
dSchwartz Guest
-
.NET Follower #7
Re: creating custom HttpContext.Current.User.Identity
hi,
at this point u dont hv the cokie with u> ((inetIdentity)HttpContext.Current.User.Identity). EmailAddress
so i tkink u shud add
If (Request.IsAuthenticated)
{
((inetIdentity)HttpContext.Current.User.Identity). EmailAddress
}
--
Thanks and Regards,
Amit Agarwal
Software Programmer(.NET)
"dSchwartz" <schwartz@cableone.net> wrote in message
news:4ae1ece2.0402191211.101c71ac@posting.google.c om...news:<Ov0q8tq9DHA.1936@TK2MSFTNGP12.phx.gbl>...> "Josh" <jcarlisle@nospam-removeme.viewfusion.com> wrote in messagewould> > I've never used a custom Identity w/o a custom principal also but I>> > 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.Nam e);
>
> 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!
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ([url]http://www.grisoft.com[/url]).
Version: 6.0.593 / Virus Database: 376 - Release Date: 2/20/2004
.NET Follower Guest



Reply With Quote

