How can I obtain the domain name or full URL of the requester?
+1
A:
I'm not sure that I understand your question, but if you need the domain name of the Windows user making the call to a service operation, use this:
OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name
This will return "{domain}\{username}".
Try this and let me know what you think (you'll probably want to paste this code into an mstest project):
[TestClass]
public class AlternativeCredentials
{
// Contracts
[ServiceContract]
interface IMyContract
{
[OperationContract]
string GetUserName();
}
// Service
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
class MyService : IMyContract
{
public string GetUserName()
{
return OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;
}
}
// Client
class MyContractClient : ClientBase<IMyContract>, IMyContract
{
public MyContractClient() { }
public MyContractClient(Binding binding, string address) :
base(binding, new EndpointAddress(address)) { }
public string GetUserName()
{ return Channel.GetUserName(); }
}
#region Host
static string address = "net.tcp://localhost:8001/" + Guid.NewGuid().ToString();
static ServiceHost host;
[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext)
{
host = new ServiceHost(typeof(MyService));
host.AddServiceEndpoint(typeof(IMyContract), new NetTcpBinding(), address);
host.Open();
}
[ClassCleanup()]
public static void MyClassCleanup()
{
if (host.State == CommunicationState.Opened)
host.Close();
}
#endregion
[TestMethod]
public void UseUserNameCredentials()
{
using (MyContractClient proxy =
new MyContractClient(new NetTcpBinding(), address))
{
proxy.ClientCredentials.UserName.UserName = "MyUsername";
proxy.ClientCredentials.UserName.Password = "MyPassword";
proxy.Open();
Assert.AreEqual("EMS\\magood", proxy.GetUserName());
proxy.Close();
}
}
}
Mark Good
2009-06-02 14:52:32
I'll give this a try. Thanks
DDiVita
2009-06-05 11:19:45
After thinking about this for a minute, I am not usuing any security on the actual service. If I call this method, would it show me what user is currenlty running the process? I think it would show me the domain/username of the system account running the service. Right?
DDiVita
2009-06-05 11:23:17
In the example I added above, "EMS\magood" is my Windows identity. WCF defaults to ClientCredentialType = Windows. You can change this to Basic for TCP bindings. What kind of Binding are you using?
Mark Good
2009-06-05 11:49:10
Some of our clients need to use the BasicHTTP binding without any security. Without having to to rely on IIS to filter IP addresses, I was hoping to check the domain name of the incoming request. When I created a similar example I got back the user account that is running the service. In this case it was our interal account we use for IIS authentication OurUser\OurDomainName. I was looking for something like www.TheRequestersDomain.com
DDiVita
2009-06-08 11:51:38