Hey all,
If you didn't figure it out from the subject, I am relatively new to CSLA. I am involved in a project using CSLA and I am currently implementing login/authentication control. As per the book and instructions I have read, I have already constructed custom identity and custom principal objects. We are using the .NET login control and configured web.config to authenticate via a custom app_code MembershipProvider class. Long story short, the login works fine...it's using my custom principal login as desired however, the Csla.ApplicationContext.User is not getting set properly or doesn't reflect my custom identity- which contains properties I need such as first name, last name, user type, email etc.
In other words, once the login is successfull, I would like the Csla.ApplicationContext.User to contain all the properties I defined in my custom identity. Currently, this user object seems to just be a System.Security.Principal.GenericPrincipal. How do I accomplish this? I can provide existing code I have if necessary.
Thanks,
Mike
Hi Mike,
In your login you should set the ApplicationContext.User to your Principal object. Here is a snippet from my IMSPrincipal static Login method.
IMSIdentity
identity = IMSIdentity.GetIdentity(user); // fetch the user roles from the db if (identity.IsAuthenticated) // make sure user is ok{
IMSPrincipal principal = new IMSPrincipal(identity); // create the new Principal with userCsla.
ApplicationContext.User = principal;}
Hope this helps.
Doug
This is the current contents of my BTPrincipal Login method:
public static bool Login(string userName, string password)
{
BTIdentity identity = BTIdentity.GetIdentity(userName, password);
if (identity.IsAuthenticated)
{
BTPrincipal principal = new BTPrincipal(identity);
Csla.ApplicationContext.User = principal;
}
return identity.IsAuthenticated;
}
It is clear that I am setting the ApplicationContext.User to the principal however, Csla.ApplicationContext.User (referenced elsewhere) does not contain the properties I have exposed in my BTPrincipal object...I have custom things like First Name, Last Name, User Type, etc.
Where am I going wrong? Do I need to modify any files in the framework to accomplish this? I simply want to validate a user logging in (which works) and have access to that user's information.
When you access it you will need to cast it to your BTPrincipal object.
BTPrincipal myPrincipal = (BTPrincipal)Csla.ApplicationContext.User;
--Doug
When I do that, I get the following error:
Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'Csla.Security.BTPrincipal'.
Being new to the framework, I'm not sure what to do with this.
1. using IIS
2. have read chapter 10 and seem to be complying with it
3. in my BTPrincipal.cs Login method, Csla.ApplicationContext.User is type Csla.Security.BTPrincipal
This is what it should be however, if I reference Csla.ApplicationContext.User on any of my aspx pages, it is back to type System.Security.Principal.GenericPrincipal.
Here is what's in my global.asax, I think it's right but maybe it's not.
protected void Application_AcquireRequestState(
object sender, EventArgs e)
{
System.Security.Principal.IPrincipal principal;
try
{
principal = (System.Security.Principal.IPrincipal)HttpContext.Current.Session["CslaPrincipal"];
}
catch
{
principal = null;
}
if (principal == null)
{
// didn't get a principal from Session, so
// set it to an unauthenticated BTPrincipal
Csla.Security.BTPrincipal.Logout();
}
else
{
// use the principal from Session
Csla.ApplicationContext.User = principal;
}
}
As I mentioned before, The intellisense in VS2005 for my Csla.ApplicationContext.User does not contain any of the properties in my BTPrincipal. I'm sure there's one little thing that I missed. Generally I find that the more time I spend on a problem, the simpler the solution ends up being.
As someone mentioned earlier in the thread,
ApplicationContext.User returns an IPrincipal. If you want access to your
custom properties or methods you must cast the value to your custom type.
BTPrincipal principal =
(BTPrincipal)Csla.ApplicationContext.User;
Dim principal As BTPrincipal =
DirectCast(Csla.ApplicationContext.User, BTPrincipal)
Rocky
From: DJMikeAZ
[mailto:cslanet@lhotka.net]
Sent: Sunday, June 10, 2007 7:08 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] New to CSLA, trying to implement custom
identity
As I mentioned before, The intellisense in VS2005 for my
Csla.ApplicationContext.User does not contain any of the properties in my
BTPrincipal. I'm sure there's one little thing that I missed. Generally I find
that the more time I spend on a problem, the simpler the solution ends up
being.
Copyright (c) Marimer LLC