IIS模拟返回应用程序池用户服务帐户

时间:2013-12-16 15:13:47

标签: asp.net-mvc-4

我开发了一个MVC应用程序并托管在服务器中。我使用服务帐户作为应用程序池标识,并仅在数据库上为该服务帐户提供完全访问权限(SysAdmin)。以下是我在Web.config&中的配置。编码。

web.config中:

<authentication mode="Windows" />
 <identity impersonate="false"/>

在我的数据访问层中,我使用以下编码来获取Windows身份:

 string windowsLogin = HttpContext.Current.User.Identity.Name.ToString();

现在我的问题是,当我访问我的应用程序时,它正在使用应用程序池标识而不是Windows标识。

如果我将Web.config中的标识设置为impersonate =“true”我可以获得Windows登录但应用程序和数据库之间的通信发生在我的Windows登录而不是应用程序池标识(服务帐户)。 />
如何获取访问应用程序的用户的身份而不是IIS APPPOOL用户?

1 个答案:

答案 0 :(得分:2)

我的问题已通过以下设置得到解决。

创建应用程序池并将其与服务帐户一起运行,并将其配置为IIS中的网站。

在我的代码文件中:

string windowsLogin = HttpContext.Current.Request.ServerVariables["LOGON_USER"].ToString();

在Web.config文件中:

<authentication mode="Windows">     
</authentication>
<authorization>
  <deny users="?" />

</authorization>
<identity impersonate="false" />

注意:如果从Visual Studio IDE运行,则会出现错误。如果您从IIS发布并运行它,它将起作用。