How to get the username and password on the WCF call service side?

When I put in the credentials of the client with the following code:

myChannelFactory.Credentials.UserName.UserName = "username"; myChannelFactory.Credentials.UserName.Password = "password"; 

Then on the server side, I see that these credentials are available in

 operationContext.IncomingMessageHeaders[1] 

However, is there an even more convenient method for obtaining a username and password? All I see in OperationContext is the chaos of properties and untyped lists, and I cannot find anything that indicates where I can get it.

+6
wcf wcf-security
source share
2 answers

You can use the static Current property on the ServiceSecurityContext class to get the current ServiceSecurityContext for the called operation.

Then you can use the PrimaryIdentity property to get the user ID with the credentials passed.

However, he will not (and should not) disclose the password. If you really need a password, you will have to go down to the message level and check the headers as you saw.

+3
source share

It depends entirely on the binding you use, and there is no general answer.

For example, if you use NetNamedPipeBinding and do

 myChannelFactory.Credentials.UserName.UserName = "username"; myChannelFactory.Credentials.UserName.Password = "password"; 

you are wasting your time: client-side binding will not do anything with this data, it will not be in the message, and it will not be available at all from the service side.

The binding will only use this data if it is configured with security settings that determine the use of username and password credentials. All standard bindings that do this will use credentials for authentication, the results of which will then be displayed in the service through ServiceSecurityContext , as specified in casperOne, and will not include password data.

To support authentication, data must be transported somewhere in the message headers. Exactly where and in what form will again be dependent on the binding. Do not assume that you will always find them in operationContext.IncomingMessageHeaders[1] .

EDIT: You can create a custom binding that will give you what you are looking for.

 CustomBinding binding = new CustomBinding( ... ); binding.Elements.Insert(1, SecurityBindingElement.CreateUserNameOverTransportBindingElement()); 

On the service side, specify UserNamePasswordValidator and configure the credentials as follows:

 serviceHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Custom; servicehost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new MyUserNamePasswordValidator(); 

The username and password will be passed to the Validate method MyUserNamePasswordValidator .

WARNING. This is not a secure authentication mechanism unless you are using secure transport, as credentials are sent in a box in the message header.

+2
source share

All Articles