C# WCF pass through userName and password to another WCF service

319 Views Asked by At

I have an existing WCF service(A) that is using custom user validation. I need to write another WCF service(B) that will work as proxy between client and existing WCF service(A). Is it a way how to get access to:

ClientCredentials.UserName.UserName
ClientCredentials.UserName.Password

How to get access to credentials on B and pass it to A? Example of B service:

    public class WcfServiceB: IWcfServiceB
    {
        private WcfServiceA _serviceA;

        public WcfServiceB()
        {
            _serviceA = new WcfServiceA();
            _serviceA.ClientCredentials.UserName.UserName = userName; //how to get current userName of WcfServiceB??
            _serviceA.ClientCredentials.UserName.Password = password; //how to get current password of WcfServiceB??
        }

        public string TestMethod()
        {
            return _serviceA.TestMethod();
        }
    }

UPD: How I achived the result:

I created a custom validation class with static fields for UserName and Password

public class CustomUserCredentials : UserNamePasswordValidator
    {
        public static string UserName;
        public static string Password;

        public override void Validate(string userName, string password)
        {
            UserName = userName;
            Password = password;
        }
    }

Then in my WcfServiceB I used these user credentials to pass them to the reference service:

public class WcfServiceB: IWcfServiceB
    {
        private WcfServiceA _serviceA;

        public WcfServiceB()
        {
            _serviceA = new WcfServiceA();
            _serviceA.ClientCredentials.UserName.UserName = CustomUserCredentials.UserName;
            _serviceA.ClientCredentials.UserName.Password = CustomUserCredentials.Password;
        }

        public string TestMethod()
        {
            return _serviceA.TestMethod();
        }
    }

Maybe it's not the best implementation, but helped me to achive the result.

1

There are 1 best solutions below

0
Jiayao On

If you need to use a windows domain user password for authentication, you can use WSHttpBinding. Also set the Security.Mode property to Message and use the X509 certificate to encrypt the password sent from the client to the server.

Calling the service delivery client and password can be written as follows:

1.The client application must prompt the user for his user name and password.

public static void GetPassword(out string username, out string password)
{
    Console.WriteLine("Provide a valid machine or domain account. [domain\\user]");
    Console.WriteLine("   Enter username:");
    username = Console.ReadLine();
    Console.WriteLine("   Enter password:");
    password = Console.ReadLine();
}

2.Creates an instance of the client proxy to specify the client's certificate.

string username;
string password;

// Instantiate the proxy.
var proxy = new Service1Client();

// Prompt the user for username & password.
GetPassword(out username, out password);

// Set the user's credentials on the proxy.
proxy.ClientCredentials.UserName.UserName = username;
proxy.ClientCredentials.UserName.Password = password;

// Treat the test certificate as trusted.
proxy.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerOrChainTrust;
// Call the service operation using the proxy