ASP.NET Windows身份验证模拟问题 [英] ASP.NET Windows Authentication Impersonate Problem

查看:104
本文介绍了ASP.NET Windows身份验证模拟问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在先前的问题中,我问过如何在应用程序中使用Windows身份验证.现在可以正常工作了,用户可以使用该帐户登录,但是我有一个数据库访问场景,找不到任何内容.

In my previous questions I was asking how to use windows authentication within my application. That is now working, users can login with there account but I have one database access scenario I can't find anything on.

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

Basically I will have several servers and here is the problem.

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

On some servers they will have Windows Authentication accounts for the SQL Server database server, so using impersonate their credentials should be used. But I notice its a global setting in the web.config (not per connection) and it one case I want to use the applications (IIS or ASP) Windows Authentication account rather than the users. (Access to my configuration database)

我怎么能做到这一点?

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

Web Application is ASP.NET MVC hosted on Server 2003/2008 IIS 6/7/7.5 with clients being Windows XP and above. Using SQL Server Express/Standard 2005/2008 mixed.

推荐答案

冒充是基于站点的,或者您可以手动将其打开.恐怕您不能手动关闭它,也不能通过连接字符串来完成它.

Impersonation is on a site wide basis, or you can manually turn it on. What you can't do is manually turn it off I'm afraid, nor can it be done via the connection strings.

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

So basically turn impersonation off, then wrap the database calls when impersonation is needed like so:

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();
}

asp.net模拟的MSDN P& P指南具有更多.

这篇关于ASP.NET Windows身份验证模拟问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆