Monday, February 05, 2007

Here is a snippet I put together on using UserName token with Web Services3.

MyWebService myService = new MyWebService();
// Create the policy
Microsoft.Web.Services3.Design.Policy policy = new Microsoft.Web.Services3.Design.Policy();
policy.Assertions.Add(new Microsoft.Web.Services3.Design.UsernameOverTransportAssertion());
myService .SetPolicy(policy);
myService.Url = http://whatever the url is;
UsernameToken token = new UsernameToken(username, password, PasswordOption.SendPlainText);
ISecurityTokenManager securityTokenManager = SecurityTokenManager.GetSecurityTokenManagerByTokenType(WSTrust.TokenTypes.UsernameToken);
securityTokenManager.CacheSecurityToken(token);
myService.SetClientCredential(token);
// If your service needs Credentials for connection pass something like this
if (CredentialUserName.Length > 0)
{
System.Net.NetworkCredential cred = null;
// User Credentials
if (CredentialDomain.Trim().Length == 0)
cred = new System.Net.NetworkCredential(CredentialUserName, CredentialPassword);
else
cred = new System.Net.NetworkCredential(CredentialUserName, CredentialPassword, CredentialDomain);
System.Net.CredentialCache credCache = new System.Net.CredentialCache();
credCache.Add(new Uri(myService.Url), "Basic", cred);
if (CredentialDomain.Trim().Length > 0)
credCache.Add(new Uri(myService.Url), "Digest", cred);
credCache.Add(new Uri(myService.Url), "Negotiate", cred);
myService.Credentials = credCache;
}
// Now do your stuff

No comments:

 
Creative Commons License
Blogged Information and Code is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.