Login

Username

Password





Register   Reset password

This is a read-only archive of the forum.

# Shortie 2/27/2007 1:10 PM
Hello All,

I'm trying to check security of a user in a normal ASPX page which I use as a popup to display an printer friendly version of a content part. I don't want the whole site included so I did not inherit the ModuleBase. I try something like this:
public partial class AFBReferencePrint : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (((Cuyahoga.Core.Domain.User)this.User.Identity).HasPermission(AccessLevel.Authenticated))
{
pnlNotAvailable.Visible = false;
pnlPrint.Visible = true;
}
else
{
// Not authenticated, do not show print page
pnlNotAvailable.Visible = true;
pnlPrint.Visible = false;
}
}
}
As far as I can see the part this.User.Identity is not my Cuyaghoga user. How can I reference the Cuyahoga user and check his/her AccessLevel.

Thanks!
# martijnb 2/28/2007 2:30 AM (edited 2/28/2007 2:32 AM)
ShortieAs far as I can see the part this.User.Identity is not my Cuyaghoga user. How can I reference the Cuyahoga user and check his/her AccessLevel.

If the user is logged in, this.User.Identity is the Cuyahoga user. The AuthenticationModule (HttpModule) takes care of this. With anonymous users, this.User.Identity is just the plain Identity.
So, when casting the user to the Cuyahoga user, you first have to check if the user is authenticated (Identity.IsAuthenticated).
# Shortie 2/28/2007 7:39 AM
I changed the IF statement a bit to:
if (((Cuyahoga.Core.Security.CuyahogaPrincipal)this.User).Identity.IsAuthenticated)

By casting to the right type of the current user I can test if he or she is authenticated. When not logged in as an authenticated user it throws an error so you have to catch that one yourself.

Also keep in mind that you have to be logged in as I forget every time after compiling and I keep wondering how it doesn't work anymore :-| .

Martijn thanks for the pointer.
# martijnb 2/28/2007 2:57 PM
This is not the right way to do it. First you know you're dealing with some user that might or might not be authenticated. This user is always available via the Page.User property.
Then after checking if this user is authenticated, you test if it is a Cuyahoga user and cast it.
protected void Page_Load(object sender, EventArgs e)
{
  if (this.User.Identity.IsAuthenticated)
  {
    Cuyahoga.Core.Domain.User cuyahogaUser = this.User.Identity as Cuyahoga.Core.Domain.User
    if (cuyahogaUser != null)
    {
      // Do stuff
    }
  }
}