1

이것은 인트라넷 응용 프로그램입니다.OWIN 웹 API Windows 서비스 - Windows ID 가장,

서비스 계정으로 Windows Server에서 실행되는 WebAPI Owin 자체 호스팅 앱이 있습니다.

프런트 엔드는이 웹 API를 통해 데이터베이스와 통신하는 AngularJS입니다.

내가 원하는 것은 데이터베이스 상호 작용이 필요한 API에서 작업이 호출 될 때 사용자 자격 증명이 서비스 계정 자체가 아닌 데이터베이스에 연결될 때 사용됩니다.

Windows 인증을 사용하고 있으며 Startup 클래스의 httpListener에서 Ntlm 인증을 사용하도록 설정했으며 사용자를 인증 할 수 있습니다.

웹 API에 요청을 제출하면 Thread.CurrentPrincipal이 현재 사용자를 반환하지만 서비스 계정을 사용하여 데이터베이스에 연결하려고 시도하기 때문에 데이터베이스 연결이 실패하고 사용자가 아닌 것을 볼 수 있습니다. 신임장.

public void PerformDBOperationAsCallingUser() 
{ 
    WindowsIdentity callingUser = (WindowsIdentity)Thread.CurrentPrincipal.Identity; 

    using (WindowsImpersonationContext wic = callingUser.Impersonate()) 
    { 
     // do your impersonated work here... 
     // check your identity like this: 
     var name = WindowsIdentity.GetCurrent().Name; 
    } 
} 

것은 같은 코드의 조각을 사용하는 것이 좋을 것이다 :

방법 웹 API에서 데이터베이스에 사용자의 자격 증명을 전달하는

답변

2

당신은이 같은 호출 사용자를 가장한다 알려주세요 귀하의 작업 단위를 중심으로 decorator. 그러나 나머지 디자인에 따라 달라집니다.

+0

고마워, 저에게 도움이되었습니다. – armulator

+0

@ user1236667 듣기 좋습니다! –

0

앱에 로그인 한 사용자와 연결해야하는 경우 자격 증명을 가장하여 데이터베이스에 액세스 할 수 있습니다. 이 같은 것이 효과가있을 수 있습니다.

public static void Test() 
{ 
    WindowsIdentity ident= (WindowsIdentity)HttpContext.Current.User.Identity 
    using (WindowsImpersonationContext ctx = ident.Impersonate()) 
    { 
     using (SqlConnection con = new SqlConnection("Data Source=YourServer;Initial Catalog=YourDatabase;Persist Security Info=False;Integrated Security=SSPI")) 
     { 
      con.Open(); 
     } 
     ctx.Undo(); 
    } 
}