1

Problem :

SQ Server setup with permissions for one generic Windows Account.

Client :

WPF Application using EntityFramework.

Similar to these:

LogonUserEx, DuplicateTokenEx for Impersonation with an ObjectContext in C#

Impersonation failing for database connection

There was no clear solution in these. After trying out lot of different options and failing, I went to basics.

Created a Console App using the MSDN example

http://msdn.microsoft.com/en-us/library/chf6fbt4.aspx

Also, tried the Simple Impersonation

How do you do Impersonation in .NET?

However, I keep getting System.StackOverflowException was unhandled exception {Cannot evaluate expression because the current thread is in a stack overflow state.}

after this line:

 bool returnValue = LogonUser(userName, domainName, Console.ReadLine(),
                LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
                out safeTokenHandle);

            Console.WriteLine("LogonUser called.");

When I experimented with the LOGON_TYPE

using (SimpleImpersonation.Impersonation.LogonUser("domain", "user", "password", SimpleImpersonation.LogonType.Network))
{
   Console.WriteLine(WindowsIdentity.GetCurrent().Name);

        }

LogonType.Network is the only one which does not give exception. And it displays the new user name.

However, when I try:

using (SimpleImpersonation.Impersonation.LogonUser("domain", "user", "password", SimpleImpersonation.LogonType.Network))
        {
            Console.WriteLine(WindowsIdentity.GetCurrent().Name);  // works as expected , correct user name
            try
            {
                model.MyEntities te = new MyEntities();
                model.PMGroup p = te.PMGroups.Where(g => g.ID == 1).FirstOrDefault();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message  + Environment.NewLine  + e.InnerException.Message);
            }

        }

I get this : The underlying provider failed on Open.

Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.

I would this to connect as the new user not anon.

Update:

Impersonation works if the Project Target is .Net3.5 or below. It does not work for .Net 4 and .Net 4.5

Maybe I am approaching this problem all wrong. If you have any ideas or suggestions, please share.

Thanks

Community
  • 1
  • 1
Yousuf
  • 11
  • 2

0 Answers0