UWP 앱에서 SQL Server 데이터베이스 연결 방법
다음의 글에도 나오지만,
UWP 앱에서 SQL Server 데이터베이스 사용
; https://docs.microsoft.com/ko-kr/windows/uwp/data-access/sql-server-databases
SQL 서버로의 연결 문자열은 윈도우 통합 인증과, SQL 서버 자체의 인증 방식에 따라 다음과 같이 2가지 유형을 지정할 수 있습니다.
// 1) Connection string for using Windows Authentication.
private string connectionString =
@"Data Source=YourServerName\SQLEXPRESS;Initial Catalog=NORTHWIND;Integrated Security=SSPI";
// 2) This is an example connection string for using SQL Server Authentication.
// private string connectionString =
// @"Data Source=YourServerName\YourInstanceName;Initial Catalog=DatabaseName; User Id=XXXXX; Password=XXXXX";
샌드 박스 환경에서 실행되는 UWP 앱은 기본적으로 "윈도우 통합 인증"을 위한 권한이 없기 때문에 SQL 서버 인증을 사용해야 합니다. 그렇지 않고 통합 인증 연결 문자열을 사용하게 되면 다음과 같은 식의 예외가 발생합니다.
System.Data.SqlClient.SqlException: Failed to generate SSPI context.
ErrorCode=NoCredentials
at System.Data.SqlClient.SNI.SNIProxy.GenSspiClientContext(SspiClientContextStatus sspiClientContextStatus, Byte[] receivedBuff, Byte[]& sendBuff, Byte[] serverName)
at System.Data.SqlClient.SNI.TdsParserStateObjectManaged.GenerateSspiClientContext(Byte[] receivedBuff, UInt32 receivedLength, Byte[]& sendBuff, UInt32& sendLength, Byte[] _sniSpnBuffer)
at System.Data.SqlClient.TdsParser.SNISSPIData(Byte[]
그렇긴 한데, 원한다면 통합 인증을 사용할 수 있는 방법이 있습니다.
Can't connect to database running on SQL Server on host from UWP
; https://github.com/dotnet/corefx/issues/22890
위의 덧글에 따라 Package.appxmanifest 파일에서 "Enterprise Authentication" 항목을 활성화시키면 됩니다.
이 옵션은 윈도우에 로그인한 사용자의 권한을 UWP 앱에서도 사용할 수 있도록 하므로 SQL 서버로의 통합 인증을 가능하게 만듭니다.
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]