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

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

我是 WCF 新手,我正在尝试在 IIS 上部署我的 WCF 示例应用程序,此应用程序在 VS2008 的调试模式下运行良好,此应用程序使用以下代码验证 wcf 消息。我这样做了,我在 wwwroot 目录中添加了生成的 .dll web.config 和 Service.svc,并且还在 IIS 管理器中添加了连接字符串,它是

服务器=MyPC\SQLEXPRESS;数据库=MySampleDb;集成安全=true

我正在使用 Windows 集成安全性。我在数据库类中使用相同的连接字符串进行连接,但出现以下异常,

请指导我部署此应用程序在 Validater 中 public override void Validate(string userName, string password) { ValidateUser(userName, password); }

public static bool ValidateUser(string userName, string password) { if (!string.IsNullOrEmpty(userName)) { ICustomer customer = GetCustomerByUsername(userName); if (customer ==null) { 抛出新异常(“未找到用户。”); } else { 返回 true; }

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

}

公共静态 ICustomer GetCustomerByUsername(string username) { 尝试 { //ConnectionString= "Server=MyPC\SQLEXPRESS;Database=MySampleDb;Integrated Security=true";

打开连接();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 需要一个打开且可用的连接。连接的当前状态为关闭。在 System.Data.SqlClient.SqlConnection.GetOpenConnection(字符串方法)在 System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(字符串方法,SqlCommand 命令)在 System.Data.SqlClient.SqlCommand.ValidateCommand(字符串方法,布尔异步)在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔 returnStream,字符串方法,DbAsyncResult 结果)在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔 returnStream,字符串方法)在 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior 行为,字符串方法)在System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) 位于 Aschrafi.MobileZollServer.Services.DatabaseHelper.GetCustomerByUsername(String username) 中

我认为我在数据库设置或 IIS 管理器网站设置中遗漏了某些点。如果能提供一些有关在 iis 中部署 wcf 和验证 wcf 通信的教程或文章链接,我将不胜感激。提前致谢。

答案1

集成安全性意味着连接在执行打开操作的线程的凭据下进行。通常,线程具有进程凭据,在 IIS 和 WCF 的情况下,意味着 AppPool 配置的凭据以作为其运行。如果线程正在模拟(WCF 中经常出现这种情况),则线程具有调用者的凭据,并且受约束的授权发生这种情况是为了向远程数据库服务器进行身份验证。无论使用什么凭据,它们都必须得到数据库服务器的信任并允许连接。

因此,问题的解决方案取决于您正在做的事情,您提供了很多代码,但没有提供实际相关的信息。

  • 你模仿吗?
  • 如果你的 WCF 服务不是模拟调用者,然后配置为在 IIS 下运行 WCF 服务的应用程序池必须被授予连接 DB 所需的权限。
    • 如果您的 WCF 应用程序池使用域帐户,请将数据库权限授予域帐户
    • 如果您的 WCF 应用程序池使用本地帐户,并且数据库与 IIS 托管在同一主机上,则需要授予本地帐户连接权限
    • 如果你的 WCF 应用程序池使用本地帐户,并且数据库远离 IIS 主机,那么你就无法连接(镜像帐户是不是支持的选项)
    • 如果您的 WCF 应用程序池使用 LocalSystem 或 NETWORK SERVICE,并且数据库位于 IIS 远程,则必须向 IIS 主机的计算机帐户授予权限
    • 如果您的 WCF 应用程序池使用 LocalSystem 或 NETWORK SERVICE,并且数据库是本地的,则需要授予 localsystem 帐户权限
    • 如果你的 WCF 应用程序池使用 LOCAL SERVICE 并且 DB 与 IIS 隔离,则你无法连接
  • 如果 WCF 模拟并且 DB 是本地的,那么您需要授予调用者连接权限
  • 如果 WCF 模拟并且 DB 处于远程状态,则需要授予调用者连接权限配置约束委派。

所有这些都在产品文档中详细描述,按照 MSDN 进行操作应该不会有任何问题:-WCF 安全基础知识WCF Security Fundamentals -使用 WCF 进行委托和模拟 -WCF 安全指南

答案2

你没有显示实际打开连接的位置。它看起来像是被关闭了。

相关内容