Skip to content

Instantly share code, notes, and snippets.

@pisceanfoot
Last active September 28, 2022 15:32
Show Gist options
  • Save pisceanfoot/a36897e24c56adcdeb9a to your computer and use it in GitHub Desktop.
Save pisceanfoot/a36897e24c56adcdeb9a to your computer and use it in GitHub Desktop.
WCF behavior, UserNameClientCredentials
/*
*client.ChannelFactory.Endpoint.Behaviors.Remove<System.ServiceModel.Description.ClientCredentials>();
*client.ChannelFactory.Endpoint.Behaviors.Add(new CustomCredentials());
*client.ClientCredentials.UserName.UserName = username;
*client.ClientCredentials.UserName.Password = password;
*/
public class UserNameClientCredentials : ClientCredentialsElement
{
private ConfigurationPropertyCollection properties;
/// <summary>
/// Username (required)
/// </summary>
public string UserName
{
get { return (string)base["userName"]; }
set { base["userName"] = value; }
}
/// <summary>
/// Password (optional)
/// </summary>
public string Password
{
get { return (string)base["password"]; }
set { base["password"] = value; }
}
protected override ConfigurationPropertyCollection Properties
{
get
{
if (properties == null)
{
ConfigurationPropertyCollection baseProps = base.Properties;
baseProps.Add(new ConfigurationProperty(
"userName",
typeof(String),
null,
null,
new StringValidator(1),
ConfigurationPropertyOptions.IsRequired));
baseProps.Add(new ConfigurationProperty(
"password",
typeof(String),
""));
properties = baseProps;
}
return properties;
}
}
protected override object CreateBehavior()
{
//var creds = (ClientCredentials)base.CreateBehavior();
//creds.UserName.UserName = UserName;
//if (Password != null) creds.UserName.Password = Password;
//ApplyConfiguration(creds);
//return creds;
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = UserName;
if (Password != null) credentials.UserName.Password = Password;
this.ApplyConfiguration(credentials);
return credentials;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment