locked
Impersonation across domain RRS feed

  • Question

  • Hi,

    I want to write a file to server inside a domain from my web application, but the server where my web application is hosted, is out of that domain. I am trying to use impersonation but it is giving this error: Logon failure: unknown user name or bad password.

    Below is my code to impersonate:

            private bool impersonateValidUser(String userName, String domain, String password)
            {
                WindowsIdentity tempWindowsIdentity;
                IntPtr token = IntPtr.Zero;
                IntPtr tokenDuplicate = IntPtr.Zero;
    
                if (RevertToSelf())
                {
                                    
                    if (LogonUser(userName, domain, password, LOGON_TYPE_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, ref token) != 0)
                    {
                       
                        if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                        {
                           
                            tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                            impersonationContext = tempWindowsIdentity.Impersonate();
                            if (impersonationContext != null)
                            {
                                CloseHandle(token);
                                CloseHandle(tokenDuplicate);
                                
                                return true;
                            }
                        }
                    }
                    
                }
                if (token != IntPtr.Zero)
                    CloseHandle(token);
                if (tokenDuplicate != IntPtr.Zero)
                    CloseHandle(tokenDuplicate);
                return false;
            }

    I have tried with LOGON32_PROVIDER_DEFAULT and LOGON32_PROVIDER_WINNT50 

    but unfortunately no luck with those settings as well. Can anyone help me identify this is impersonation from out of the domain. 

    Tuesday, October 1, 2013 1:53 PM

Answers

  • Hi AsifHameed1,

    This forum is to discuss problems of C# development. You can consider posting it in ASP.NET forum for more efficient responses.http://forums.asp.net

    I have done a search for your question. This article describes different ways to implement impersonation in an ASP.NET application. http://support.microsoft.com/kb/306158/en-us

    However you must ensure your user name and password are correct.

    Thanks.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    • Edited by Eason_H Wednesday, October 2, 2013 1:54 AM
    • Marked as answer by Eason_H Monday, October 7, 2013 1:27 AM
    Wednesday, October 2, 2013 1:54 AM