5

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.

gideon
  • 19,329
  • 11
  • 72
  • 113
NomNomNom
  • 811
  • 3
  • 12
  • 37
  • We can't show you anything unless you show us how you are storing the data in generic principal in the first place. – gideon Apr 24 '13 at 04:17
  • I have edited my post with code, that is how i stored my data into generic principal, and that code is inside global asax file. – NomNomNom Apr 24 '13 at 04:24
  • Ok. You're missing one more thing, `I can't get user profile data from generic principal when I want to use it.` => **When do you want to use it**? Inside an aspx page? – gideon Apr 24 '13 at 04:54
  • yep, i want to display user profile data in an aspx file called dafult.aspx, i want to put its code in page_load, this file called after user already authenticated. – NomNomNom Apr 24 '13 at 05:00
  • @HendraLim Ok I added an answer. – gideon Apr 24 '13 at 05:09

2 Answers2

10

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;
     }
    
gideon
  • 19,329
  • 11
  • 72
  • 113
2

You can also use the following code:

FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
string userData = id.Ticket.UserData
Ger Groot
  • 1,071
  • 11
  • 7