简体   繁体   English

如何使 SOAP API 请求需要某些字段?

[英]How to make some fields to be required for SOAP API Requests?

I have made a WCF Service in order to make a SOAP API.为了制作 SOAP API,我制作了一个 WCF 服务。
I want to make these fields required: Username , Password , ID我想让这些字段成为必需: UsernamePasswordID
How can I make them to be required fields when a client make a request to the API?当客户端向 API 发出请求时,如何使它们成为必填字段?

I want to :我想要 :

  • make them required for every API Request , and when the request does not contain one of them to send an ERROR.使每个 API Request 都需要它们,并且当请求不包含其中之一时发送 ERROR。
  • check them in order to see if the client has send the correct Credentials , an if one of them is wrong to send an ERROR.检查它们以查看客户端是否发送了正确的 Credentials ,如果其中一个错误发送错误。
    For Example :例如 :
    If their value must be :如果它们的值必须是:
  • Username = user Username = 用户
  • Password = pass Password = 通过
  • ID = IdNumber ID = 身份证号码

This is the code for them so far:到目前为止,这是他们的代码:

[OperationContract]
Credentials CheckCredentials(Credentials credentials);
[DataContract]
public class Credentials
{
    [DataMember]
    public string Username { get; set; }

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

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

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

}
public Credentials CheckCredentials(Credentials credentialsCheck)
{
    if (credentialsCheck.Username == "user" && 
        credentialsCheck.Password == "pass" && 
        credentialsCheck.ID == "IdNumber")
    {
        credentialsCheck.ErrorResult = "Correct";

        return credentialsCheck;
    }
    else
    {
        credentialsCheck.ErrorResult = "Wrong";

        return credentialsCheck;
    }

}

Any other suggestions would be gratefully accepted.任何其他建议将不胜感激。

UserName authentication is provided in WCF, which will require the client to provide username and password when calling the service, here is a demo: WCF中提供了UserName认证,这会要求客户端在调用服务时提供用户名和密码,这里是一个演示:

Create CustomUserNameValidator class:创建 CustomUserNameValidator 类:

public class CustomUserNameValidator : System.IdentityModel.Selectors.UserNamePasswordValidator
        {
            public override void Validate(string userName, string password)
            {
                if (null == userName || null == password)
                {
                    throw new ArgumentNullException();
                }

                if (!(userName == "test1" && password == "test1") && !(userName == "test2" && password == "test2"))
                {
                    throw new FaultException("Unknown Username or Incorrect Password");
                }
            }
        }

Add the userNameAuthentication node in the configuration file:在配置文件中添加userNameAuthentication节点:

 <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Microsoft.Samples.UserNamePasswordValidator.CalculatorService+CustomUserNameValidator, service"/>

The client needs to provide username and password when calling:客户端调用时需要提供用户名和密码:

  proxy.ClientCredentials.UserName.UserName = "test1";
  proxy.ClientCredentials.UserName.Password = "test1";

If you still need to verify the ID, you need to consider using Custom Token.如果还需要验证ID,则需要考虑使用Custom Token。 For how to Create a Custom Token, you can refer to this link:关于如何创建自定义Token,可以参考这个链接:

https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/how-to-create-a-custom-token https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/how-to-create-a-custom-token

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM