When connecting to with default Windows authentication, if you do not want to connect as the currently logged-on user, you can alter the code to connect as a different user.
Before you begin
Connecting as another user requires one of the following:
- You have the user name and password of another Windows user.
- Your current user has impersonation rights.
Procedure
- To use a Windows user name and password, use the following code:
using (ChannelFactory<ISessionAwareCoreService> factory =
new ChannelFactory<ISessionAwareCoreService>("netTcp_2011"))
{
ISessionAwareCoreService client = factory.CreateChannel();
client.Impersonate(@"domain\user");
Console.WriteLine(client.GetCurrentUser().Title);
}
where domain is the domain that contains the user you want to connect to and user is the name of that user.
- To impersonate a Windows user, use the following code:
using (ChannelFactory<ISessionAwareCoreService> factory =
new ChannelFactory<ISessionAwareCoreService>("netTcp_2011"))
{
NetworkCredential networkCredential =
new NetworkCredential("username", "password", "domain");
factory.Credentials.Windows.ClientCredential = networkCredential;
ISessionAwareCoreService client = factory.CreateChannel();
Console.WriteLine(client.GetCurrentUser().Title);
}
where domain is the domain that contains the user you want to connect to , user is the name of that user, and password is that user's password.