ADグループを使用してSQL Serverデータベースにアクセスして認証するC#アプリケーションがありますが、 Webアプリケーションと同じネットワークサービスアカウントは、現在Webサーバーアプリケーションプールを使用しています。 WindowsアプリケーションはWebサーバーとアプリケーションプールを経由しないため、Webアプリケーションと同じネットワークサービスアカウントを使用するようにWindowsアプリケーションを構成する方法は不明です。私はSO、MSDN、Googleを検索しましたが、何も見つかりませんでした。これを行う方法はありますか?助けてくれてありがとう。ネットワークサービスアカウントを使用してWindows C#アプリケーションをSQL Serverに接続する方法はありますか
1
A
答えて
0
アプリケーションをネットワークサービスとして実行します(このアカウントではなりすましは機能しません)。 hereを説明するように
- Sysinternalsの使用PsExec.exeは、ネットワークサービスとしてアプリケーションを実行するために:私たちは、クライアントアプリケーション(コンソール、WindowsフォームやWPF)について話していると仮定すると、私は3つのオプションを参照してください。
- アプリケーションを2つに分割 - データアクセスを実装し、ネットワークサービスとして実行するWindowsサービスと、そのサービスと通信し、UIおよびアプリケーションレイヤを実装するクライアントアプリケーション
- データアクセスをWebサービスとして実装して実行するアプリケーションプールの[ネットワークサービスの資格情報]を開き、アプリケーションがそのWebサービスを使用してデータにアクセスできるようにします。ほとんどの場合、この方法が望ましいと思いますが、深刻なリファクタリングを必要とする可能性があります。
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
偽装が動作するかどうか不明です。私はそれを前に調べて、少なくとも私たちの場合、データベースチームがネットワークサービスアカウントへのパスワードを私たちに提供しないという最大の問題を発見しました。それは良い提案であり、その背後にあるアイデアのように、この制限を回避する手段はありません。 –
関連する問題
- 1. C++/CLIを使用してSQL Serverに接続する方法
- 2. Windows認証接続文字列を使用してOLEDBを使用してSQL Serverに接続する方法
- 3. Windowsアカウントを使用してSymfony 2アプリケーションをSQL SERVERに接続します
- 4. Rを使用してSQL Serverに接続する方法は?
- 5. Management Studio 2005を使用してMS SQL Server 2008に接続する方法はありますか?
- 6. 目的のCを使用してSQL Serverに接続する方法
- 7. Eclipseで直接AndroidアプリケーションをSQL Serverに接続する方法(Monodroidアプリケーションが接続しています)
- 8. C#でIPアドレスを使用してアプリケーションをSQL Serverに接続する方法は?
- 9. MVC/Cを使用してエンドユーザーSQL Server接続ダイアログを作成する方法
- 10. Visual Studio 2017でコンパイルしたC#アプリケーションを使用してSQL Server 2008に接続する方法はありますか?
- 11. ODBCデータ接続を使用してローカルSQL Serverに接続する方法は? (ログインに失敗しました)
- 12. OE10.2Bx86を使用してSQL Server x64をProgressに接続する方法は?
- 13. Windows AzureアプリケーションをSQL Server Compact Editionバージョン3.5.8080に接続する方法
- 14. Windows認証を使用してManagement StudioでSQL Serverに接続します。
- 15. MonoDroidを使用してAndroidサーバーからSQL Serverに接続する方法
- 16. SQL Serverの接続C#:認証Windowsの代わりにコンピュータ名を使用
- 17. golangでGORMを使用してMicrosoft SQL Serverに接続する方法は?
- 18. PHPを使用してSQL Serverデータベースに接続する方法は?
- 19. jdbcを使用してSQL Serverのローカルホストに接続する方法は?
- 20. SQL Server Management Studioを使用して外部データベースに接続する方法は?
- 21. AFPを使用してWindowsクライアントをOSX Serverに接続する方法
- 22. asp.net WebアプリケーションをSQL Serverに接続する方法
- 23. C#exeを使用してSQL Serverに接続
- 24. C#を使用してSQL Serverデータベースに接続できない
- 25. WindowsアプリケーションからSQL Serverに接続できません
- 26. powershellを使用してSQL Serverに接続します。
- 27. mssqlモジュールを使用してNode.JSからWindows認証でSQL Serverに接続する方法
- 28. Windows認証を使用してSSMSでリモートSQL Serverに接続する方法は?
- 29. Windows 2008サーバーにasp.netとpowershellを接続する方法はありますか?
- 30. Windowsサービスを使用してSQL Server Expressデータベースとリモートデータベースを同期する方法はありますか。
ありがとうございました。 #1は、SQLサーバー上のアカウントにパスワードがあり、担当チームがそのパスワードを提供しないため、パスワードが必要なので動作しないように見えます。 #2と#3は正しいでしょうが、私が正しいとすれば#2はパスワードが必要ですが、#3はWebサービスがアプリケーションプールの認証を通過することを許可しません。残念ながら、これは簡単な作業ではないことは間違いありません。 –
NetworkServiceアカウントにパスワードがありません。しかし、説明したようにpsexecを使用するには特権が必要になると思います。とにかく、それは速くて簡単な一時的な回避策かもしれませんが、特にユーザーのコンピュータで永久的な解決策としては良くありません。 NetworkServiceアカウントの詳細:https://msdn.microsoft.com/en-us/library/windows/desktop/ms684272(v=vs.85).aspx –