c# - SOAP header 身份验证不起作用?

标签 c# .net web-services soapheader

我正在使用 SOAP header ,并且我需要从数据库对其进行身份验证。因此,我尝试创建一个类并使用一种方法,当我们传递用户名和密码时,它将返回它是否存在于数据库中。

----- My Main Class ---
[WebMethod, SoapHeader("AuthenticateUser")]
public System.Xml.XmlElement CancelUSer(string _UserID, string _Remarks)
{

    if (UsersAuth.ValidateUser(AuthenticateUser.UserName, AuthenticateUser.Password) > 0)
    {
//METHODS

     }
 }

public class UserAuthenticateHeader : SoapHeader
{
public string UserName;
public string Password;

}

public class UsersAuth
{
static OracleConnection con;
public UsersAuth()
{
 con = new OracleConnection(WebConfigurationManager.ConnectionStrings["conString"].ToString());
}




public static int ValidateUser(string _UserName, string _Password)
{
    int Result = 0;
    using (OracleCommand cmd = new OracleCommand("SELECT COUNT(*) FROM USES WHERE UID=:UID AND PASSWORD=:PASSWORD", con)) {
        cmd.Parameters.AddWithValue(":UID", _UserName);
        cmd.Parameters.AddWithValue(":PASSWORD", _Password);
        con.Open();
        Result = Convert.ToInt32(cmd.ExecuteScalar());
        con.Close();
    }
    return Result;
}

}

现在,当我从程序中调用它时,它会将 ObjectReferenceNotSet 抛出到对象的实例。我已经为应用程序中的方法设置了用户名、密码和 header 值。但它没有命中“ValidateUser”方法。

有什么方法可以实现这一目标吗?

最佳答案

只要构造函数是静态的,您就可以在构造函数中保留连接对象实例化。即

public static UsersAuth()
{
    con = new OracleConnection(WebConfigurationManager.ConnectionStrings["conString"].ToString());
}

这样做的优点是只创建一个连接,而不是为每个方法调用创建一个新连接。您需要考虑是否需要重用连接,如果需要,是否以及何时应该关闭连接。

关于c# - SOAP header 身份验证不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11644812/

相关文章:

c# - 使用 ksoap 协议(protocol)不匹配的 Soap web 服务和 android 客户端

c# - SOAP 返回数据,但 C# 返回空响应

c# - 启用 System.Timers.Timer 和 GarbageCollector

asp.net - IIS url 重写,损坏的 href(css、js 等)

c# - 文件正在被另一个进程错误使用

c# - 无限期地在任务上调用 .ContinueWith 是模拟队列和循环线程的有效方法吗?

java - tomee webservice 未部署 - asm 错误 - 无法为 Web 模块 simple-webservice 创建注释扫描器 : 43626

c# - WCF 数据服务 - 在返回结果之前修改对象?

c# - 登录验证始终返回 true

c# - 用另一个写一个可执行文件