Max
Max

Reputation: 1299

How to display the username credentials used in a SOAP WCF call?

The SOAP call below works fine using my credentials but when other people use my .EXE which calls the WCF service I get a 401 denied. I am trying to find out what are the credentials being passed.

I could look at the IIS logs but am trying to do it programatically, thanks:

   public static Guid GetServerID(string serverName, string soapUrl)
    {
        Guid result;
        try
        {
            Guid vServerId = new ControllerWS.Controller
            {
                Url = soapUrl,
                Timeout = Config.SoapCallTimeOut,
                Credentials = CredentialCache.DefaultCredentials
            }.GetServerId(serverName);
            result = vServerId;

            //Console.WriteLine("CredentialCache.DefaultCredentials: " + CredentialCache.DefaultCredentials.ToString());
            //ICredentials Credentials = CredentialCache.DefaultCredentials.GetCredential()
        }

Upvotes: 0

Views: 331

Answers (2)

Gregor Primar
Gregor Primar

Reputation: 6805

It realy depends what type of authentication you are using. When windows credentials with impersonation then you can get username like this:

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name

If you are using plain username and password than those values can be stored inside request header and read like this:

MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;
string userId = headers.GetHeader<Guid>("MyKey", "MyNamespce");

Upvotes: 1

Mike Parkhill
Mike Parkhill

Reputation: 5551

You can use an IDispatchMessageInspector on the server side to intercept the full message including headers. From there you can inspect the credentials that are being passed in.

Here's a blog post outlining how to log the full message from the message inspector including the configuration steps you need to do to wire it up.

Upvotes: 0

Related Questions