2016-07-06 1 views
1

DBAは、ドメインaccout(ユーザーは "user"、ドメインは "domain"、パスワードは "password")をアサインしましたが、データベースにアクセスできません。私がコードをデバッグすると、データベースにアクセスして正しいデータを返すことができますが、asynはありますか?私はそれがなぜ、次のコードがわかりません、あなたは私を助けることができますか?EFドメイン単位でデータベースにアクセスする方法

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: パブリックBaseTypeContext() :塩基( "名前=試験") {Impersionate impersionate = Impersionate.Create( "ユーザ"、 "ドメイン "、"パスワード ")。 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(); 
} 
} 
+0

ウエッ、calss不要にdomaimアカウントを追加する必要がありますアプリ? –

+0

会社はこれが好きなのですが、より安全を意味します。私はSQL認証を使用する前に行いますが、dbaがドメインアカウントに変更すると、私は変更する必要があります。 – Daniel

答えて

0

私はその理由を知って、私はIISサーバーのアプリケーションプール使用のウェブサイト、およびImpersionate

だけのドメインアカウントSQL認証またはセットアップを使用しない理由
関連する問題