使用Windows身份验证进行数据库连接-使用哪个用户?

问题描述 投票:0回答:2

我有一个ASP.NET Web API应用程序,当前连接到数据库时使用SQL身份验证。我想将连接更改为使用Windows身份验证。这样做时,如何指定该Web应用程序以及数据库访问使用的用户?我正在使用IIS v8.5.9600.16384

asp.net-web-api windows-authentication iis-8
2个回答
2
投票

我不能说我同意将集成用于应用程序数据库访问,因为这使安全性更具挑战性,并且编码问题与始终必须为每个用户处理不同的priv的可能性有关,但显然我不愿意不知道您情况的所有要求。

所以回答您的问题:

  • 如果您的连接字符串设置为Intergated Security,则执行线程的身份用于提供凭据。

  • 默认情况下,ASP.NET辅助进程的身份将是与身份相关联的网络凭据。

  • 您可以这样查看凭据:

    IPrincipal threadPrincipal = Thread.CurrentPrincipal;
    Console.WriteLine("Name: {0}\nIsAuthenticated: {1}" +
        "\nAuthenticationType: {2}", 
        threadPrincipal.Identity.Name, 
        threadPrincipal.Identity.IsAuthenticated,
        threadPrincipal.Identity.AuthenticationType);
    

您可以通过web.config中的impersonation或以编程方式设置身份。

Web.config:

<identity impersonate="true" userName="accountname" password="password" />

代码:(使用HttpContext用户的凭据)这也假定IIS也使用集成的语言

System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext = 
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();

//User is the HttpContext.User
//Insert your code that runs under the security context of the authenticating user here.

impersonationContext.Undo();  

0
投票

您将必须关闭匿名身份验证并通过IIS管理器启用Windows身份验证。

© www.soinside.com 2019 - 2024. All rights reserved.