views:

140

answers:

2

This is the function that I use in web service for getting current windows user.

 <WebMethod()> _
  Function User() As String
        Dim p() As String = Split(My.User.Name, "\")
        Dim p1 As String = p(1)
        Return p1
    End Function

When I run service on localhost it realy return current windows user name! The problem is when i run service from remote PC, in that case I got nothing from this function. What is problem with this service, and how I can get name of Windows user?

Thanks!

+3  A: 

Which do you expect it to return: the identity of the caller? or the identity of the service account that is running the web-service?

In most cases the windows identity would depend on how the server is configured; is it using impersonation? Plus are you passing the callers identity over when calling the service? For example, UseDefaultCredentials or Credentials. And is the service configured to recognise the users claims?

Generally I try not to use impersonation; it requires elevation at the server, doesn't support all environments, and can have a big impact on the effectiveness of pooling - so I wouldn't expect the windows identity to flow, but if you are passing the callers identity in some form I might expect the "principal" (Thread.CurrentPrincipal) to represent the caller.

I can't remember from asmx, but with WCF you can write your own code to setup the principal from your own authentication scheme if you want.

Marc Gravell
Thanks for response! I have this like situation: Web service is set on server! I just need when client computer run this service, that client get windows logon user name from server computer! I really don't no what is problem i am able to get Computer name of server but not user name. I suppose that problem is somewhere in security settings!
Comii
@Comii - in IIS, this is the identity of the App-Pool that is hosting the application.
Marc Gravell
A: 

You can get the identity the current thread is running under by the below line of code:

System.Threading.Thread.CurrentPrincipal.Identity.Name
ChrisNel52
Thanks for response! It's return noting when it run from remote computer, just like my code!
Comii