Search code examples
.netwindowswcfcrystal-reportsimpersonation

Impersonation In Crystal Reports


I've been trying to update a WCF project that interacts with Crystal Reports to get it to connect to a MS SQL Server via Windows Authentication. It is a WCF project but is running as a Windows Service and not through IIS.

I am using SimpleImpersonation to do the impersonation step. Essentially, if the correct values are provided in the incoming SOAP request, including IntegratedSecurity=true, it impersonates another user and tries to log in with IntegratedSecurity.

My code looks something like this:

using (Impersonation.LogonUser(domain, username, password, LogonType.NewCredentials)) {
    reportDocument.Execute();
}

I know impersonation is working because I've printed System.Security.Principal.WindowsIdentity.GetCurrent().Name before and after the using statement and saw the username change to what I want after the using.

I have tried each of the different LogonTypes but I get the same results.

I get a Database logon failed error message from calling Execute(). However, if I start the web service running as the account I'm trying to impersonate, I am able to connect to the database properly.

My questions are:

  • Is Impersonation working the way I'm intending it to? Am I doing something wrong? (It doesn't look like it based off of the documentation)
  • Is there something with the Crystal Reports runtime for .NET that I need to configure differently?

Solution

  • It turns out my issue was not with impersonation, but with a DB driver. I found a comment on https://archive.sap.com/discussions/thread/3621859:

    Please check the file "sqlncli.dll" exist in server or not? .. not sure this file or some other dll.

    I installed https://www.microsoft.com/en-us/download/confirmation.aspx?id=50402 on the server and my code started working automagically.

    I wish the error messages returned from Crystal Reports were a bit more descriptive, but oh well, at least it's working.