【问题标题】:how can I create authenticating web service soap in c#如何在 C# 中创建身份验证 Web 服务肥皂
【发布时间】:2015-02-04 12:28:31
【问题描述】:

为了连接到soap web 服务,我打算对我的客户进行身份验证,但我找不到任何相关信息。在我看来,基于主体的唯一方法可以是 Soapheader。 根据我阅读的解释,我编写了如下代码:

IService.cs
public interface IService
{
    [OperationContract]
    bool DoWork();
}

Service.cs
public class Service : IService
{
    public AuthHeader Authentication;


    [SoapHeader("Authentication", Required = true)]
    [WebMethod(Description = "Returns some sample data")]
    public bool DoWork()
    {
        if (Authentication.Username == "userName" &&
        Authentication.Password == "pwd")
        {
            //Do your thing
            return true;

        }
        else
        {
            //if authentication fails
            return false;
        }
    }
}

主要问题是我启动项目时,方法的Header部分没有参数。

<s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:header>
    <action s:mustunderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService/DoWork</action>
</s:header>
<s:body>
    <dowork xmlns="http://tempuri.org/" />
</s:body>
</s:envelope>

请告诉我如何从客户端获取网络服务的用户名和密码? 如果你有其他方法,请告诉我。 感谢您的合作。

【问题讨论】:

  • 每个人都错过了您正在混合 WCF 和 ASMX。 [WebMethod] 是旧的 ASMX 技术的一部分,你不应该在新的开发中使用它。请搜索“wcf 身份验证”了解如何使用身份验证创建 WCF 服务。

标签: c# web-services wcf authentication soap


【解决方案1】:

我将首先包含 WCFExtras (https://wcfextras.codeplex.com/)。

然后您可以执行以下操作:

1。定义 AuthHeader-Class

[DataContract]
public class AuthHeader
{
    [DataMember]
    public string Username { get; set; }

    [DataMember]
    public string Password { get; set; }
}

2。 IService 内部:将 [SoapHeaders] 添加到您的 [ServiceContract](包含 WCFExtras 后,SoapHeaders 可用)。还将 [SoapHeader...] 添加到 [OperationContract]

[ServiceContract]    
[SoapHeaders]
public interface IService
{
    [OperationContract]
    [WCFExtras.Soap.SoapHeader("AuthHeader", typeof(AuthHeader))]
    bool DoWork();
}

3。在 Service.cs 中使用 SoapHeaderHelper(WCFExtras 的一部分):

public class Service : IService
{
    [WebMethod(Description = "Returns some sample data")]
    public bool DoWork()
    {
        AuthHeader authentication = SoapHeaderHelper<AuthHeader>.GetInputHeader("AuthHeader");
        if (@"userName".Equals(authentication.Username) && @"pwd".Equals(authentication.Password))
        {
            //Do your thing
            return true;

        }
        else
        {
            //if authentication fails
            return false;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多