2017-10-31 2 views
1

ADグループを使用してSQL Serverデータベースにアクセスして認証するC#アプリケーションがありますが、 Webアプリケーションと同じネットワークサービスアカウントは、現在Webサーバーアプリケーションプールを使用しています。 WindowsアプリケーションはWebサーバーとアプリケーションプールを経由しないため、Webアプリケーションと同じネットワークサービスアカウントを使用するようにWindowsアプリケーションを構成する方法は不明です。私はSO、MSDN、Googleを検索しましたが、何も見つかりませんでした。これを行う方法はありますか?助けてくれてありがとう。ネットワークサービスアカウントを使用してWindows C#アプリケーションをSQL Serverに接続する方法はありますか

答えて

0

アプリケーションをネットワークサービスとして実行します(このアカウントではなりすましは機能しません)。 hereを説明するように

  1. Sysinternalsの使用PsExec.exeは、ネットワークサービスとしてアプリケーションを実行するために:私たちは、クライアントアプリケーション(コンソール、WindowsフォームやWPF)について話していると仮定すると、私は3つのオプションを参照してください。
  2. アプリケーションを2つに分割 - データアクセスを実装し、ネットワークサービスとして実行するWindowsサービスと、そのサービスと通信し、UIおよびアプリケーションレイヤを実装するクライアントアプリケーション
  3. データアクセスをWebサービスとして実装して実行するアプリケーションプールの[ネットワークサービスの資格情報]を開き、アプリケーションがそのWebサービスを使用してデータにアクセスできるようにします。ほとんどの場合、この方法が望ましいと思いますが、深刻なリファクタリングを必要とする可能性があります。
+0

ありがとうございました。 #1は、SQLサーバー上のアカウントにパスワードがあり、担当チームがそのパスワードを提供しないため、パスワードが必要なので動作しないように見えます。 #2と#3は正しいでしょうが、私が正しいとすれば#2はパスワードが必要ですが、#3はWebサービスがアプリケーションプールの認証を通過することを許可しません。残念ながら、これは簡単な作業ではないことは間違いありません。 –

+0

NetworkServiceアカウントにパスワードがありません。しかし、説明したようにpsexecを使用するには特権が必要になると思います。とにかく、それは速くて簡単な一時的な回避策かもしれませんが、特にユーザーのコンピュータで永久的な解決策としては良くありません。 NetworkServiceアカウントの詳細:https://msdn.microsoft.com/en-us/library/windows/desktop/ms684272(v=vs.85).aspx –

0

偽装を使用する必要があります。私たちは、カスタムクラスを使用して、当社のWebアプリケーションでこれを使用します。

Imports System.Web 
Imports System.Web.Security 
Imports System.Security.Principal 
Imports System.Runtime.InteropServices 

''' <summary>Creates an impersonation session if needed</summary> 
''' <remarks> 
'''  This is a clever little object, it is designed to facilitate the 'using' functionality to handle the initialisation and disposal of the impersonation 
'''  context. It will only impersonate if the connection is set up to use windows authentication otherwise it will do nothing 
''' 
'''  Recommended usage 
'''   using new impersonate(domain, username, password) 
'''    Do database Stuff 
'''   end using 
''' </remarks> 
Public Class Impersonate 
    Implements IDisposable 


Private isImpersonating As Boolean = False 

Private Sub Impersonate(ByVal domain As String, ByVal username As String, ByVal password As String) 
    If Me.impersonateValidUser(username, domain, password) Then 
     ' all is well 
    Else 
     ' not so well, raise exception 
     Throw New System.Exception("Unable to use provided AD authentication to connect to database") 
    End If 
End Sub 

#Region "IDisposable Support" 
    Private disposedValue As Boolean ' To detect redundant calls 

    ' IDisposable 
    Protected Overridable Sub Dispose(ByVal disposing As Boolean) 
     If Not Me.disposedValue Then 
      If disposing Then 
       ' clean up the impersonation 
       undoImpersonation() 
      End If 

     End If 
     Me.disposedValue = True 
    End Sub 

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources. 
    'Protected Overrides Sub Finalize() 
    ' ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. 
    ' Dispose(False) 
    ' MyBase.Finalize() 
    'End Sub 

    ' This code added by Visual Basic to correctly implement the disposable pattern. 
    Public Sub Dispose() Implements IDisposable.Dispose 
     ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. 
     Dispose(True) 
     GC.SuppressFinalize(Me) 
    End Sub 
#End Region 



#Region "Impersonate" 

Dim LOGON32_LOGON_INTERACTIVE As Integer = 2 
Dim LOGON32_PROVIDER_DEFAULT As Integer = 0 

Dim impersonationContext As WindowsImpersonationContext 

Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, _ 
         ByVal lpszDomain As String, _ 
         ByVal lpszPassword As String, _ 
         ByVal dwLogonType As Integer, _ 
         ByVal dwLogonProvider As Integer, _ 
         ByRef phToken As IntPtr) As Integer 

Declare Auto Function DuplicateToken Lib "advapi32.dll" (_ 
         ByVal ExistingTokenHandle As IntPtr, _ 
         ByVal ImpersonationLevel As Integer, _ 
         ByRef DuplicateTokenHandle As IntPtr) As Integer 

Declare Auto Function RevertToSelf Lib "advapi32.dll"() As Long 
Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long 


Public Sub Page_Loada(ByVal s As Object, ByVal e As EventArgs) 
    If impersonateValidUser("username", "domain", "password") Then 
     'Insert your code that runs under the security context of a specific user here. 
     undoImpersonation() 
    Else 
     'Your impersonation failed. Therefore, include a fail-safe mechanism here. 
    End If 
End Sub 

Private Function impersonateValidUser(ByVal userName As String, _ 
ByVal domain As String, ByVal password As String) As Boolean 

    Dim tempWindowsIdentity As WindowsIdentity 
    Dim token As IntPtr = IntPtr.Zero 
    Dim tokenDuplicate As IntPtr = IntPtr.Zero 
    impersonateValidUser = False 

    If CType(RevertToSelf(), Boolean) Then 
     If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, 
        LOGON32_PROVIDER_DEFAULT, token) <> 0 Then 
      If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then 
       tempWindowsIdentity = New WindowsIdentity(tokenDuplicate) 
       impersonationContext = tempWindowsIdentity.Impersonate() 
       If Not impersonationContext Is Nothing Then 
        impersonateValidUser = True 
       End If 
      End If 
     End If 
    End If 
    If Not tokenDuplicate.Equals(IntPtr.Zero) Then 
     CloseHandle(tokenDuplicate) 
    End If 
    If Not token.Equals(IntPtr.Zero) Then 
     CloseHandle(token) 
    End If 
End Function 

Private Sub undoImpersonation() 
    If Me.isImpersonating Then 
     impersonationContext.Undo() 
    End If 
End Sub 

#End Region 
End Class 

あなたは、その後のようなコードを使用してクラスを呼び出す:私はネットワークサービスをしているとSQLに接続するための唯一の方法だと思い

using new impersonate(domain, username, password) 
    ' Do database Stuff 
end using 
+0

偽装が動作するかどうか不明です。私はそれを前に調べて、少なくとも私たちの場合、データベースチームがネットワークサービスアカウントへのパスワードを私たちに提供しないという最大の問題を発見しました。それは良い提案であり、その背後にあるアイデアのように、この制限を回避する手段はありません。 –

関連する問題