简体   繁体   中英

Using a unique Windows Id in a WCF service environment

I have a Silverlight application that uses WCF. I want to be able to have unique information based on the Windows user that's using the application; ie, I want to show only data for that user. The site will be hosted on IIS. I'm running into various problems with this...

I was originally using HttpContext.Current but that started coming back as null when I hosted it in IIS (despite using Windows authentication).

I have in my web.config:

<system.web>
    <authentication mode="Windows"/>
  <system.web>

The only thing I'm wondering about is the Silverlight clientconfig details. The bindings for that have <security mode="None"/> but whenever I try and change them I get an error.

I am currently using basicHttpBinding.

I have also set the aspNetCompatibility for my service, so it should be able to handle HttpContext.

Here's my ServiceReferences.ClientConfig file:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="winAuthBinding" maxBufferSize="2147483647"
            maxReceivedMessageSize="2147483647">
          <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="Windows" proxyCredentialType="Windows"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

    <client>
      <endpoint address="../service.svc"
          binding="basicHttpBinding" bindingConfiguration="winAuthBinding"
          contract="Contract1" name="name1" />
      <endpoint address="../service.svc"
          binding="basicHttpBinding" bindingConfiguration="winAuthBinding"
          contract="Contract2" name="name2" />
      <endpoint address="../service.svc"
          binding="basicHttpBinding" bindingConfiguration="winAuthBinding"
          contract="Contract3" name="name3" />
    </client>
  </system.serviceModel>

And the web.config; I'll only paste the system.serviceModel section:

<system.serviceModel>

    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehaviour">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"  />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

    <bindings>
      <basicHttpBinding>
        <binding name="winAuthBinding">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

    <services>
      <service behaviorConfiguration="serviceBehaviour" name="name1">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="winAuthBinding"
                  contract="Contract1"/>
      </service>
      <service behaviorConfiguration="serviceBehaviour" name="name2">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="winAuthBinding"
                  contract="Contract2"/>
      </service>
      <service behaviorConfiguration="serviceBehaviour" name="name3">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="winAuthBinding"
                  contract="Contract3"/>
      </service>
    </services>

    <client/>
  </system.serviceModel>

Have you configured the <basicHttpBinding> element for the IIS hosted service, it should resemble:

<security mode="TransportCredentialOnly">
  <transport clientCredentialType="Windows" />
</security>

Not sure if this is what you need but have you tried

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]

Decoration on your service class?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM