ASP.NET MVC3 IIS6允许域用户访问的网站,但另一个用户连接数据库
-
25-09-2019 - |
题
MVC 3(可能是相同的1和2),服务器2003,IIS 6,.NET 4,SQL Server 2005的
我有一个简单的管理应用程序,只有2人或3人将永远登录,所以我不想和窗体身份验证,独立的用户名/密码和所有打扰。我想简单的让用户登录其现有的AD用户/密码。但是我想通过在已经选定DB相应权限的专用帐户连接到SQL Server。
我受够了以下方案做到了这一点在传统的ASP
IIS: Enable anonymous Access: checked. Username/pw dedicated account for the SQL Server DB
Integrated Windows authentication: unchecked
Digest authentication: unchecked
Basic authentication: unchecked
.NET Passport authentication: unchecked
在传统的ASP应用程序接受贴出HTML形式,然后经由LDAP认证
' Establish connection
Set objConnection = Server.CreateObject("ADODB.Connection")
' Connection properties
objConnection.Provider = "ADsDSOOBJECT"
objConnection.Properties("User ID") = strUsername
objConnection.Properties("Password") = strPassword
objConnection.Properties("Encrypt Password") = True
' Open connection
objConnection.open "DS Query", strUsername, strPassword ' user supplied user/pw
' Query Active Directory
strQuery = "SELECT cn FROM 'LDAP://" & strDomain & "' WHERE objectClass='*' "
Set objCommand = server.CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = strQuery
' Get recordset
Set objRS = objCommand.Execute
' If no record of user found
If objRS.BOF Or objRS.EOF Then
' User does not exist
AuthenticateUser = False
Else
' User is authentic
AuthenticateUser = True
End If
我想在.NET此相同的行为,但没有HTML形式。似乎可能的是,当用户访问与[授权]属性的控制器:
[Authorize(Users = @"domain\user1, domain\user2, domain\user3")]
,他们应该被提示输入他们的用户AD / PW(或不与IE,取决于区和自动登录选项)然后进行认证,但使用IIS专用用户连接到DB。如何做到这一点?
我一直认为模仿是要走的路,所以这是我的web.config:
<system.web>
<identity impersonate="true"/>
<authentication mode="Windows" />
我尝试添加这一点,但没有帮助:
<anonymousIdentification enabled="false"/>
<authorization>
<allow users="*"/>
<deny users="?"/>
</authorization>
我可以搞定一切为我工作,如果我使用基本身份验证,但这只是因为我的帐户还可以访问数据库。如果谁在用户=列出,但其他用户没有访问DB尝试,他们收到:
Cannot open database "Dbname" requested by the login. The login
失败。 登录失败,用户域\用户3'。
显然模拟没有在这里做的事情。
所以,这已是如何将一般的Windows网络上的防火墙后面验证一个相当典型的例子。什么是最好的方法呢?我已经使用类似 LDAP身份验证的ASP代码被认为是自AuthorizeAttribute。净MVC 这似乎更接近的东西在传统的ASP应用程序是如何工作的,但由于某些原因,我想我可能只是让一切单独设置和configuation工作。
<强> EDIT1:强>
阅读一些巴里多兰斯初ASP.NET安全昨晚之后,看来,我可以改变应用程序池的用户和有是打的SQL Server数据库用户。那正确吗?或者是在打DB的网站设置匿名用户?
试图改变应用程序池的用户,但该帐户不是IIS_WPG组的一部分。这是必需的,对吧?
<强> EDIT2:强> 与基本认证检查,匿名与指定用户检查,得到以下:
User.Identity.Name: DOMAIN\jcurran
User.Identity.IsAuthenticated: True
User.Identity.AuthenticationType: Basic
WindowsIdentity.GetCurrent().AuthenticationType: Kerberos
WindowsIdentity.GetCurrent().Name: DOMAIN\jcurran
WindowsIdentity.GetCurrent().IsAuthenticated: True
WindowsIdentity.GetCurrent().IsAnonymous: False
WindowsIdentity.GetCurrent().IsGuest: False
WindowsIdentity.GetCurrent().IsSystem: False
WindowsIdentity.GetCurrent().ImpersonationLevel.ToString(): Impersonation
Thread.CurrentPrincipal.Identity.Name: DOMAIN\jcurran
使用基本身份验证未选中,匿名与指定的用户检查,我得到一个空白页。使用Fiddler收率检查:
HTTP/1.1 401 Unauthorized
Date: Thu, 06 Jan 2011 18:02:42 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 3.0
Cache-Control: private
Content-Length: 0
解决方案
假设你要访问使用Windows身份验证,然后的更改应用程序池标识是要走的路。
在web.config中,您需要一套impersonate="false"
或ASP.NET将尝试使用客户端的凭据连接到数据库,而不是应用程序池的(也就是它看起来像模拟是做什么你问它做的事)。
在IIS您必须启用集成的Windows身份验证,如果你愿意,你可以禁用匿名访问。