Impersonation of forms-authenticated Active Directory user

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default Impersonation of forms-authenticated Active Directory user

    Hello all

    I wonder if the great and the good of this esteemed forum might shed
    some light on a problem of mine...

    Three servers in a domain: one Active Directory server, one SQL Server
    and one IIS. IIS hosts an ASP.NET Web Application which requires that
    users log on through a web form, are authenticated against their
    Active Directory account and then acquire the permissions on the SQL
    Server objects that their Active Directory group membership bestows.

    In the following code authentication through LDAP works and authTicket
    appears to be generated correctly. At this stage User.Identity is
    empty, but by loading the page a second time User.Identity contains
    the correct details. This is presumably as a result of reading the
    cookie, but how can I get the correct User.Identity from the
    authTicket without letting the cookie reader do it for me
    automagically?

    Anyway, even on the refresh when we have...

    User.Identity.Name=myuser
    User.Identity.IsAuthenticated=True
    User.Identity.AuthenticationType=Forms

    ....the code still fails on
    (System.Security.Principal.WindowsIdentity)User.Id entity, producing
    'specified cast is invalid'. Is this because its authentication type
    is Forms? If so, and given that form based login is a requirement, how
    can I "Impersonate the Authenticating User in Code".


    string adPath = "LDAP://ad1.mydomain.com/DC=mydomain,DC=com";
    LdapAuthentication adAuth = new LdapAuthentication(adPath);
    if(true == adAuth.IsAuthenticated(txtDomainName.Text,
    txtUserName.Text, txtPassword.Text))
    {
    FormsAuthenticationTicket authTicket =
    new FormsAuthenticationTicket(1,
    txtUserName.Text,
    DateTime.Now,
    DateTime.Now.AddMinutes(60),
    false, "");
    string encryptedTicket =
    FormsAuthentication.Encrypt(authTicket);
    HttpCookie authCookie =
    new HttpCookie(FormsAuthentication.FormsCookieName,
    encryptedTicket);
    Response.Cookies.Add(authCookie);
    System.Security.Principal.WindowsImpersonationCont ext
    impersonationContext;
    impersonationContext =
    ((System.Security.Principal.WindowsIdentity)User.I dentity).Impersonate();
    }

    As you may recognise, this code has been cribbed from
    [url]http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q306158[/url] and it
    may help diagnosis to know that the code from the "Impersonate a
    Specific User in Code" section is working fine, but presumably this
    approach would require me to carry the username and password around,
    in the session say, and re-authenticate on every page_load.

    Once the user has logged I want every page to be executed in the
    context of their AD account, so should perhaps there's some altogether
    better way of achieving this that I'm missing.

    Cheers,
    Mike.
    Mike Swift Guest

  2. Similar Questions and Discussions

    1. problem accesing Active Directory from an ASP.NET App when user has been authenticated via AD certificate mapping
      hello, I am developing an ASP.NET web application which interacts withAD. Client/User authentication must be done via AD certificatemapping, so I...
    2. Asp.Net Forms authentication using Active Directory
      Hi. I am using forms authentication in ASP.Net against Active Directory. I have followed the example provided by Microsoft. Here is my problem....
    3. Forms Authentication with Active Directory using vb.net
      I have seen many examples of form authentication using c#. Can someone point me to a sample using vb.net. I would like to use WinNT://domain,...
    4. Forms or windows authentication with active directory?
      Hi, I'm having a hard time deciding (figuring out) how to implement security in my asp.net application. Requirements: - Use active directory as...
    5. Impersonation failure with Index Server and Forms Authentication with Active Directory
      I'm posting my problem experience and solution I found here for other ASP.NET developers. I have a web application that uses Forms Authentication...
  3. #2

    Default Re: Impersonation of forms-authenticated Active Directory user

    This isn't going to work. You can't cast a FormsPrincipal to a
    WindowsPrincipal.

    In order to get a WindowsPrincipal, you must either use Windows auth in
    ASP.NET/IIS or explicitly call the LogonUser API with the user's credentials
    in order to create a token that you can then use to create a WindowsIdentity
    that you can impersonate. For the latter, the canonical example is here,
    but it can't be used easily on Win2K due to security restrictions:

    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemSecurityPrincipalWindowsImpersonationCo ntextClassTopic.asp?frame=true[/url]

    If you use Windows auth in ASP.NET, you will also need to be careful about
    impersonation and double hop issues.

    HTH,

    Joe K.

    "Mike Swift" <mikeswift@mailinator.com> wrote in message
    news:88fce4c8.0404280552.20fee3f6@posting.google.c om...
    > Hello all
    >
    > I wonder if the great and the good of this esteemed forum might shed
    > some light on a problem of mine...
    >
    > Three servers in a domain: one Active Directory server, one SQL Server
    > and one IIS. IIS hosts an ASP.NET Web Application which requires that
    > users log on through a web form, are authenticated against their
    > Active Directory account and then acquire the permissions on the SQL
    > Server objects that their Active Directory group membership bestows.
    >
    > In the following code authentication through LDAP works and authTicket
    > appears to be generated correctly. At this stage User.Identity is
    > empty, but by loading the page a second time User.Identity contains
    > the correct details. This is presumably as a result of reading the
    > cookie, but how can I get the correct User.Identity from the
    > authTicket without letting the cookie reader do it for me
    > automagically?
    >
    > Anyway, even on the refresh when we have...
    >
    > User.Identity.Name=myuser
    > User.Identity.IsAuthenticated=True
    > User.Identity.AuthenticationType=Forms
    >
    > ...the code still fails on
    > (System.Security.Principal.WindowsIdentity)User.Id entity, producing
    > 'specified cast is invalid'. Is this because its authentication type
    > is Forms? If so, and given that form based login is a requirement, how
    > can I "Impersonate the Authenticating User in Code".
    >
    >
    > string adPath = "LDAP://ad1.mydomain.com/DC=mydomain,DC=com";
    > LdapAuthentication adAuth = new LdapAuthentication(adPath);
    > if(true == adAuth.IsAuthenticated(txtDomainName.Text,
    > txtUserName.Text, txtPassword.Text))
    > {
    > FormsAuthenticationTicket authTicket =
    > new FormsAuthenticationTicket(1,
    > txtUserName.Text,
    > DateTime.Now,
    > DateTime.Now.AddMinutes(60),
    > false, "");
    > string encryptedTicket =
    > FormsAuthentication.Encrypt(authTicket);
    > HttpCookie authCookie =
    > new HttpCookie(FormsAuthentication.FormsCookieName,
    > encryptedTicket);
    > Response.Cookies.Add(authCookie);
    > System.Security.Principal.WindowsImpersonationCont ext
    > impersonationContext;
    > impersonationContext =
    >
    ((System.Security.Principal.WindowsIdentity)User.I dentity).Impersonate();
    > }
    >
    > As you may recognise, this code has been cribbed from
    > [url]http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q306158[/url] and it
    > may help diagnosis to know that the code from the "Impersonate a
    > Specific User in Code" section is working fine, but presumably this
    > approach would require me to carry the username and password around,
    > in the session say, and re-authenticate on every page_load.
    >
    > Once the user has logged I want every page to be executed in the
    > context of their AD account, so should perhaps there's some altogether
    > better way of achieving this that I'm missing.
    >
    > Cheers,
    > Mike.

    Joe Kaplan \(MVP - ADSI\) Guest

  4. #3

    Default Re: Impersonation of forms-authenticated Active Directory user

    just wanted to find out why User.Identity is empty for the first time..
    r u populating User.Identity with GenericPrinciple object for the first time
    just after validating from your login page?

    Av.

    "Mike Swift" <mikeswift@mailinator.com> wrote in message
    news:88fce4c8.0404280552.20fee3f6@posting.google.c om...
    > Hello all
    >
    > I wonder if the great and the good of this esteemed forum might shed
    > some light on a problem of mine...
    >
    > Three servers in a domain: one Active Directory server, one SQL Server
    > and one IIS. IIS hosts an ASP.NET Web Application which requires that
    > users log on through a web form, are authenticated against their
    > Active Directory account and then acquire the permissions on the SQL
    > Server objects that their Active Directory group membership bestows.
    >
    > In the following code authentication through LDAP works and authTicket
    > appears to be generated correctly. At this stage User.Identity is
    > empty, but by loading the page a second time User.Identity contains
    > the correct details. This is presumably as a result of reading the
    > cookie, but how can I get the correct User.Identity from the
    > authTicket without letting the cookie reader do it for me
    > automagically?
    >
    > Anyway, even on the refresh when we have...
    >
    > User.Identity.Name=myuser
    > User.Identity.IsAuthenticated=True
    > User.Identity.AuthenticationType=Forms
    >
    > ...the code still fails on
    > (System.Security.Principal.WindowsIdentity)User.Id entity, producing
    > 'specified cast is invalid'. Is this because its authentication type
    > is Forms? If so, and given that form based login is a requirement, how
    > can I "Impersonate the Authenticating User in Code".
    >
    >
    > string adPath = "LDAP://ad1.mydomain.com/DC=mydomain,DC=com";
    > LdapAuthentication adAuth = new LdapAuthentication(adPath);
    > if(true == adAuth.IsAuthenticated(txtDomainName.Text,
    > txtUserName.Text, txtPassword.Text))
    > {
    > FormsAuthenticationTicket authTicket =
    > new FormsAuthenticationTicket(1,
    > txtUserName.Text,
    > DateTime.Now,
    > DateTime.Now.AddMinutes(60),
    > false, "");
    > string encryptedTicket =
    > FormsAuthentication.Encrypt(authTicket);
    > HttpCookie authCookie =
    > new HttpCookie(FormsAuthentication.FormsCookieName,
    > encryptedTicket);
    > Response.Cookies.Add(authCookie);
    > System.Security.Principal.WindowsImpersonationCont ext
    > impersonationContext;
    > impersonationContext =
    >
    > ((System.Security.Principal.WindowsIdentity)User.I dentity).Impersonate();
    > }
    >
    > As you may recognise, this code has been cribbed from
    > [url]http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q306158[/url] and it
    > may help diagnosis to know that the code from the "Impersonate a
    > Specific User in Code" section is working fine, but presumably this
    > approach would require me to carry the username and password around,
    > in the session say, and re-authenticate on every page_load.
    >
    > Once the user has logged I want every page to be executed in the
    > context of their AD account, so should perhaps there's some altogether
    > better way of achieving this that I'm missing.
    >
    > Cheers,
    > Mike.

    avnrao Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139