WCF应用程序部署在IIS中,但SQL Server数据库连接不工作

我是新的WCF,我想在IIS上部署我的WCF示例应用程序,这个应用程序在VS2008的debugging模式下工作正常,此应用程序使用以下代码validationwcf的消息。 我正在这样做,我已经在wwwroot目录中添加了生成的.dlls web.config和Service.svc,并且我还在IISpipe理器中添加了连接string,它是

Server = MyPC \ SQLEXPRESS; Database = MySampleDb;集成安全性= true

我正在使用Windows集成安全性。 我在数据库类中使用相同的连接string连接,但我得到以下exception,

请指导我部署此应用程序在Validator公共覆盖无效validation(string用户名,string密码){ValidateUser(用户名,密码); }

public static bool ValidateUser(string userName,string password){if(!string.IsNullOrEmpty(userName)){ICustomer customer = GetCustomerByUsername(userName); if(customer == null){throw new exception(“User Not found。”); } else {return true; }

} else { throw new FaultException("User name is required!"); } 

}

public static ICustomer GetCustomerByUsername(string username){try {// ConnectionString =“Server = MyPC \ SQLEXPRESS; Database = MySampleDb; Integrated Security = true”;

OpenConnection的(); var cmd = new SqlCommand(“GetUserByUsername”,_connection){CommandType = CommandType.StoredProcedure};

  cmd.Parameters.Add("Username", username); connState = _connection.State.ToString(); if (_connection.State == ConnectionState.Closed) OpenConnection(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); ICustomer customer = null; customer = ExtractCustomerFromDataReader(dr)[0]; dr.Close(); return customer; } catch (Exception e) { throw new Exception(e.Message + Environment.NewLine + e.StackTrace); } finally { CloseConnection(); } 

}例外:

ExecuteReader需要一个开放和可用的连接。 连接的当前状态是closures的。 System.Data.SqlClient.SqlConnection.GetOpenConnection(String method)System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method,SqlCommand command)at System.Data.SqlClient.SqlCommand.ValidateCommand(String method,Boolean async)at System.Data.SqlClient.SqlConnection.GetOpenConnection(String method)在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,string方法)上的.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,string方法,DbAsyncResult结果)。 DataSqlClient.SqlCommand.ExecuteReader(CommandBehavior行为,string方法)在System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior行为)在Aschrafi.MobileZollServer.Services.DatabaseHelper.GetCustomerByUsername(string用户名)在

我想我在数据库设置或IISpipe理器的网站设置中缺less一些点。 一些教程或atricle链接在iis的wcf部署和authenticationwcf通信将非常感激。 提前致谢。

集成的安全性意味着连接在执行打开操作的线程的凭证下发生。 通常,线程具有进程凭证,在IIS和WCF的情况下,AppPoolconfiguration的凭证运行为。 如果线程正在模拟(WCF经常出现这种情况),那么该线程将拥有调用者的凭据,并发生约束委派以便与远程数据库服务器进行身份validation。 无论使用什么凭证,都必须信任并允许通过数据库服务器进行连接。

所以你的问题的解决scheme取决于你在做什么,你提供了很多的代码,但不是实际的相关信息。

  • 你假装吗?
  • 如果您的WCF服务不模拟调用者,则configuration为在IIS下运行WCF服务的应用程序池必须被授予在数据库上连接所需的权限。
    • 如果您的WCF应用程序池使用域帐户,则将该数据库的权限授予该域帐户
    • 如果您的WCF应用程序池使用本地帐户,并且数据库托pipe在与IIS相同的主机上,则需要为本地帐户授予连接权限
    • 如果您的WCF应用程序池使用本地帐户,并且该数据库远离IIS主机,则无法连接(镜像帐户不是受支持的选项)
    • 如果您的WCF应用程序池使用LocalSystem或NETWORK SERVICE,并且该数据库远离IIS,则必须授予IIS主机的计算机帐户权限
    • 如果您的WCF应用程序池使用LocalSystem或NETWORK SERVICE,并且该数据库是本地的,则需要授予本地系统帐户权限
    • 如果您的WCF应用程序池使用本地服务,而数据库是IIS的rmeote,则无法连接
  • 如果WCF模拟和DB是本地的,那么你需要授予连接到调用者
  • 如果WCF模拟和数据库是rmeote,那么你需要授予连接调用程序, configuration约束委派。

所有这些在产品文档中都有详细的描述,您不应该在MSDN之后有任何问题: – WCF安全基础知识 – 使用WCF委派和模仿 – WCF安全指南

您不显示您实际打开连接的位置。 它像它closures的lokos。