I have made a WCF Service in order to make a SOAP API.
I want to make these fields required: Username
, Password
, ID
How can I make them to be required fields when a client make a request to the API?
I want to :
Username
= user Password
= pass ID
= IdNumber 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:
Create CustomUserNameValidator class:
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 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. For how to Create a Custom Token, you can refer to this link:
https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/how-to-create-a-custom-token
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.