I just got a comment from an old post from over 2 years ago about some custom auth problems I had. It's funny looking back that far to see how you were. As with all typical developers, I look back and laugh at myself. 🙂
The question posted was if I could share my custom authentication code. The fact of the matter is, the way I was doing authentication at the time was kind of silly and I thought I'd post how we do it now. It's much simpler and uses everything that's already built into ASP.NET 2.0 already. When a user logs into your site (using whatever type of authentication you want), the Context.User (same User object that shows up on the Page class, etc) is set to an IPrincipal. Depending on what "username" you passed in for it to tack on to the cookie, you'll be ablel to access a key for looking up more detailsl about the user. Then the same applies from the rest of my 2 year old post. Create a BasePage class that inherits from Page and shadow the User property with your own. Here's the code from our platform in the BasePage class.
{
get { return Request.IsAuthenticated; }
}
private string username;
public string Username
{
get
{
if (username == null)
{
if (IsAuthenticated)
username = base.User.Identity.Name;
else
username = "";
}
return username;
}
}
private bool isUserSet;
private EvNetUser user;
public new EvNetUser User
{
get
{
if (!isUserSet)
{
if (IsAuthenticated)
user = Users.Retrieve(Username);
isUserSet = true;
}
return user;
}
}
Now anywhere in our site we can say Page.User and get back an object filled with everything we need to know about the current user. If the request is anonymous, Page.User will return null. Hope this helps Mohammed!