Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get custom data out of generic principal inside an aspx code behind?

I am building an application and integrating it with active directory.

So I authenticate my app user to active directory users, after that I store some of user's data like : user group and user profile to Generic principal and user identity to Generic identity.

My problem is I can't get user profile data from generic principal when I want to use it.

Can someone show me how to do that?

 string cookieName = FormsAuthentication.FormsCookieName;
 HttpCookie authCookie = Context.Request.Cookies[cookieName];

 if (authCookie == null)
 {
       //There is no authentication cookie.
       return;
 }

 FormsAuthenticationTicket authTicket = null;

 try
 {
       authTicket = FormsAuthentication.Decrypt(authCookie.Value);
 }
 catch (Exception ex)
 {
      //Write the exception to the Event Log.
       return;
 }

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

 String data = authTicket.UserData.Substring(0, authTicket.UserData.Length -1);
 string[] userProfileData =   data.Split(new char[] { '|' });
 //Create an Identity.
 GenericIdentity id = 
                  new GenericIdentity(authTicket.Name, "LdapAuthentication");
 //This principal flows throughout the request.
 GenericPrincipal principal = new GenericPrincipal(id, userProfileData);
 Context.User = principal;

Note : Code above is in global asax file, and I want to use user profile data which I stored in generic principal in another file named default.aspx.

like image 455
NomNomNom Avatar asked Apr 24 '13 04:04

NomNomNom


2 Answers

So first off you shouldn't be doing this:

GenericPrincipal principal = new GenericPrincipal(id, userProfileData);
                                                     //^^ this is wrong!!

The second argument to the constructor is for Roles. See the docs.


If you want to store data into the generic principal then what you should do is

  1. Create a class out of GenericIdentity :

    class MyCustomIdentity : GenericIdentity
    {
      public string[] UserData { get; set;}
      public MyCustomIdentity(string a, string b) : base(a,b)
      {
      }
    }
    
  2. Create it like this:

    MyCustomIdentity = 
               new MyCustomIdentity(authTicket.Name,"LdapAuthentication");
                                                      //fill the roles correctly.
    GenericPrincipal principal = new GenericPrincipal(id, new string[] {});
    
  3. Get it in the page like this:

    The Page class has a User property.

    So for example in Page load you could do this:

     protected void Page_Load(object sender, EventArgs e) {
      MyCustomIdentity id =  (MyCustomIdentity)this.User.Identity
      var iWantUserData = id.UserData;
     }
    
like image 110
gideon Avatar answered Nov 19 '22 15:11

gideon


You can also use the following code:

FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
string userData = id.Ticket.UserData
like image 35
Ger Groot Avatar answered Nov 19 '22 15:11

Ger Groot