ASP.NET Windows身份验证模拟问题

Phi*_*hil 2 asp.net iis windows-authentication

在我以前的问题中,我问的是如何在我的应用程序中使用Windows身份验证.现在正在运行,用户可以使用该帐户登录,但我有一个数据库访问方案,我找不到任何内容.

基本上我会有几台服务器,这就是问题所在.

在某些服务器上,他们将拥有SQL Server数据库服务器的Windows身份验证帐户,因此应使用模拟其凭据.但我注意到它在web.config(不是每个连接)中的全局设置,并且我想要使用应用程序(IIS或ASP)Windows身份验证帐户而不是用户.(访问我的配置数据库)

我怎么能实现这个目标?

Web应用程序是在Server 2003/2008 IIS 6/7/7.5上托管的ASP.NET MVC,客户端是Windows XP及更高版本.使用SQL Server Express/Standard 2005/2008混合.

blo*_*art 6

模拟是在站点范围内进行的,或者您可以手动将其打开.你不能做的是手动关闭我害怕,也不能通过连接字符串完成.

所以基本上关闭模拟,然后在需要模拟时包装数据库调用,如下所示:

using System.Security.Principal;

WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;
WindowsImpersonationContext ctx = null;
try
{
    ctx = winId.Impersonate();
    // Do your thing
}
catch
{
}
finally
{
    if (ctx != null)
        ctx.Undo();
}
Run Code Online (Sandbox Code Playgroud)

MSDN P&P导向到asp.net模拟有更多.