locked
How to get windows user name with domain name RRS feed

  • Question

  • Hi,

    I have a WCF service that is called from an silverlight application.In this service I need to get current windows user, who is invoking this service.I tried to us WindowsIdentity.GetCurrent().Name but it returns NT AUTHORIT/SYSTEM. Also I tried ServiceSecurityContext.Current but it returns null.Anybody help me to get current windows identity from the WCF service.


    Muhammed Shakeer

    Friday, August 24, 2012 8:59 AM

Answers

  • Hi,

    Can you give me more info, like on the hosting you use, the environment.

    Also, try to use wsHttpBinding to see if this works with it.

    Try to mark the implementation of the method you want to use impersonation with
    [OperationBehavior(Impersonation=ImpersonationOption.Required)]

    Don't forget when you make all these changes to regenerate the proxy you use to connect.

    Monday, August 27, 2012 11:13 AM
  • Hi All,

    Thanks for the support.Using wsHttpBinding instead of basicHttpBinding solves the issue.Now I am getting ServiceSecurityContext.Current.


    Muhammed Shakeer

    Tuesday, August 28, 2012 4:56 AM

All replies

  • Hi,

    Did you try impersonation on your wcf service?

    http://msdn.microsoft.com/en-us/library/ms730088.aspx

    Friday, August 24, 2012 9:33 AM
  • Hi,

    I tried to use Imperative model for Impersonation using ServiceSecurityContext.Current.WindowsIdentity.  But  ServiceSecurityContext.Current.WindowsIdentity is returning null. Anything I missed in the configuration. My configuration is as like below.

    <configuration>

      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior>
               <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
            <behavior name="TestService_Behavior">
              <serviceDebug includeExceptionDetailInFaults="false" />
              <serviceMetadata httpGetEnabled="true" />
              <serviceThrottling maxConcurrentCalls="500"
            maxConcurrentInstances ="100"
          maxConcurrentSessions ="200"/>
            </behavior>       
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
        <services>
          <service behaviorConfiguration="TestService_Behavior"
            name="TestServiceImplementation.HelloTestService">
            <endpoint address="" binding="basicHttpBinding" name="Endpoint2"
              bindingNamespace="http://TestService.Service/2012"
              contract="TestServiceImplementation.IHelloTestService"/>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>
        </services>
      </system.serviceModel>
     <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>
     
    </configuration>


    Muhammed Shakeer

    Friday, August 24, 2012 10:01 AM
  • Hi,

    What's the behavior on your service if you add this piece of code in the method called by the client?

    using (ServiceSecurityContext.Current.WindowsIdentity.Impersonate())
    {
         return string.Format("Hello, {0}", WindowsIdentity.GetCurrent().Name);
    }

    Sunday, August 26, 2012 6:53 PM
  • Hi Hakim,

    In my case ServiceSecurityContext.Current.WindowsIdentity is returning null. So, if I add this piece of code in my service, it will cause null reference exception.


    Muhammed Shakeer

    Monday, August 27, 2012 4:06 AM
  • Hi,

    Is there a particular reason why you use basicHttpBinding?

    Anyway, try adding this in your configuration file

    <bindings>

    <basicHttpBinding>

        <binding name="MyAuthBinding">

            <security mode="TransportCredentialOnly">

                <transport clientCredentialType="Windows"/>

            </security>

        </binding>

    </basicHttpBinding>

    <bindings>

    and in your endpoint add bindingConfiguration="MyAuthBinding"

    Let me know


    • Edited by SisuHak Monday, August 27, 2012 8:57 AM wrongly clicked on submit
    Monday, August 27, 2012 8:54 AM
  • Hi Hakim,

    I have already tried this.But no use.Still it is returning null.


    Muhammed Shakeer

    Monday, August 27, 2012 10:16 AM
  • Hi,

    Can you give me more info, like on the hosting you use, the environment.

    Also, try to use wsHttpBinding to see if this works with it.

    Try to mark the implementation of the method you want to use impersonation with
    [OperationBehavior(Impersonation=ImpersonationOption.Required)]

    Don't forget when you make all these changes to regenerate the proxy you use to connect.

    Monday, August 27, 2012 11:13 AM
  • Hello, It looks some how windowsIdentity is not working, can you try with PrimaryIdentity and check what you get
    ServiceSecurityContext.Current.PrimaryIdentity.Name
    HTH



    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

    Monday, August 27, 2012 11:42 AM
  • Hi All,

    Thanks for the support.Using wsHttpBinding instead of basicHttpBinding solves the issue.Now I am getting ServiceSecurityContext.Current.


    Muhammed Shakeer

    Tuesday, August 28, 2012 4:56 AM