EF如何通过域帐户访问数据库

时间:2016-07-06 17:03:02

标签: entity-framework

DBA已经指定了域帐户(用户是"用户",域名是"域",密码是"密码"),但我不能访问数据库,它总是显示域帐户没有被Auth,但如果我调试代码,它将可以访问数据库并返回正确的数据,是否有任何asyn?我不知道原因,以下是代码。

ConnectString中:

<add name="test"      connectionString="metadata=res://*/Models.BaseTypeContext.csdl|res://*/Models.BaseTypeContext.ssdl|res://*/Models.BaseTypeContext.msl;provider=System.Data.SqlClient;provider connection string="data source=test;initial catalog=test;integrated
    security=SSPI;MultipleActiveResultSets=True;App=EntityFramework""
    providerName="System.Data.EntityClient" />

的DbContext:         public BaseTypeContext()         :base(&#34; name = test&#34;)         {         Impersionate impersionate     = Impersionate.Create(&#34; user&#34;,&#34; domain&#34;,&#34; password&#34;);         impersionate.Open();         }

public class Impersionate : IDisposable
{
public static int LOGON32_LOGON_INTERACTIVE = 2;
public static int LOGON32_PROVIDER_DEFAULT = 0;
public static int LOGON_TYPE_NEW_CREDENTIALS = 9;
public static int LOGON32_PROVIDER_WINNT50 = 3;
private WindowsImpersonationContext impersonationContext;
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int LogonUser(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
private extern static int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
public string User { get; protected set; }
public string Domain { get; protected set; }
protected string Password { get; set; }
protected bool Running { get; set; }
private Impersionate() { }
public static Impersionate Create(string user, string domain, string password)
{
Impersionate instance = new Impersionate();
instance.User = user;
instance.Domain = domain;
instance.Password = password;
instance.Running = false;
return instance;
}
public bool Open()
{
if (Running)
return Running;
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
int ret = LogonUser(User, Domain, Password, LOGON_TYPE_NEW_CREDENTIALS,
LOGON32_PROVIDER_WINNT50, ref token);
//int ret = LogonUser(User, Domain, Password, LOGON32_LOGON_INTERACTIVE,
//LOGON32_PROVIDER_DEFAULT, ref token);
if (ret != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
Running = true;
return Running;
}
else
{
Running = false;
throw new Exception(string.Format("Impersonate error code={0}", ret));
}
}
else
{
Running = false;
throw new Exception(string.Format("Impersonate error code={0}", ret));
}
}
else
{
Running = false;
throw new Exception(string.Format("Impersonate error code={0}", ret));
}
}
public void Close()
{
if (!Running)
return;
impersonationContext.Undo();
Running = false;
}
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
protected virtual void Dispose(bool disposing)
{
if (disposing && Running)
Close();
}
}

1 个答案:

答案 0 :(得分:0)

我知道原因,我需要在IIS服务器应用程序池中添加domaim帐户所使用的网站,并且不需要calss Impersionate