我有一个在 Windows 身份验证模式下运行的 Web 服务。
我有一个应用程序来发送 asmx 连接:
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
BasicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
EndpointAddress endpoint = new EndpointAddress("http://xyzts/Service.asmx");
WS_SPIL.ServiceSoapClient client = new WS_SPIL.ServiceSoapClient(basicHttpBinding, endpoint);
client.ClientCredentials.Windows.AllowedImpersonationLevel = Sytem.Security.Principal.TokenImpersonationLevel.Impersonation;
client.ChannelFactory.Credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
我执行以下代码(在 ASMX 上): User.Identity.Name 返回我的 Windows AD 登录名。
到目前为止,一切都很好。
现在我想用用户登录连接到数据库。但我收到当前用户(IIS 用户)没有连接到 sql 服务器的权限的错误消息。
try
{
SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
csb.DataSource = "sqlservername";
csb.InitialCatalog = "DBName";
csb.IntegratedSecurity = true;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = csb.ToString();
conn.Open();
conn.Close();
return "ok";
}
catch (Exception ex)
{
return ex.ToString();
}