在我的WCF Web服务中获取WindowsIdentity

在我的WCF Web服务中获取WindowsIdentity,第1张

概述我接管了一个不再和我们在一起的开发人员的代码.它是一个最初使用传入用户名的WCF Web服务,但我们需要它来使用 WindowsIdentity. string identity = ServiceSecurityContext.Current.WindowsIdentity.Name; 该代码最终返回一个空字符串.我正在使用安全(wsHttpSecure)绑定,因此ServiceSecurity 我接管了一个不再和我们在一起的开发人员的代码.它是一个最初使用传入用户名的WCF Web服务,但我们需要它来使用 WindowsIDentity.

string IDentity = ServiceSecurityContext.Current.windowsIDentity.name;

该代码最终返回一个空字符串.我正在使用安全(wshttpSecure)绑定,因此ServiceSecurityContext.Current不是null或任何东西.我一直在寻找一天的解决方案,但还没有找到任何东西.

因为我是WCF的新手,所以我不确定其他相关信息是什么.以下是IIS中Web服务的启用身份验证设置:

Anonymous Authentication - Enabledwindows Authentication - Enabled

这是web服务的web.config:

<?xml version="1.0" enCoding="UTF-8"?><configuration>    <connectionStrings>        <clear />        <add name="LocalsqlServer" connectionString="Data Source=.\instancenameHere;Initial Catalog=default;Integrated Security=sspI;"/>    </connectionStrings>    <appSettings configSource="appSettings.config" />    <system.diagnostics>        <sources>            <source name="System.ServiceModel" switchValue="information,ActivityTracing" propagateActivity="true">                <Listeners>                    <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="c:\ServiceLogs\WebServiceLog.svclog" />                </Listeners>            </source>        </sources>    </system.diagnostics>    <system.web>        <trace enabled="true" />        <membership defaultProvIDer="XIMembershipProvIDer" userIsOnlineTimeWindow="30">            <provIDers>                <clear/>                <add name="XIMembershipProvIDer" type="LolSoftware.MIDdleTIEr.BusinessLogic.XIMembershipProvIDer"      applicationname="LolWebService"/>            </provIDers>        </membership>        <compilation deBUG="true" targetFramework="4.0" />    </system.web>    <system.serviceModel>        <clIEnt />        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />        <behaviors configSource="behaviors.config" />        <bindings configSource="bindings.config" />        <services configSource="services.config" />    </system.serviceModel>    <system.webServer>        <modules runAllManagedModulesForAllRequests="true" />        <handlers>            <remove name="svc-ISAPI-4.0_64bit"/>            <remove name="svc-ISAPI-4.0"/>            <remove name="svc-Integrated-4.0"/>            <add name="svc-ISAPI-4.0_64bit" path="*.svc" verb="*" modules="IsAPIModule" scriptprocessor="%systemroot%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isAPI.dll" resourceType="UnspecifIEd" preCondition="classicMode,runtimeVersionv4.0,bitness64" />            <add name="svc-ISAPI-4.0" path="*.svc" verb="*" modules="IsAPIModule" scriptprocessor="%systemroot%\Microsoft.NET\Framework\v4.0.30319\aspnet_isAPI.dll" resourceType="UnspecifIEd" preCondition="classicMode,bitness32" />            <add name="svc-Integrated-4.0" path="*.svc" verb="*" type="System.ServiceModel.Activation.httpHandler,System.ServiceModel.Activation,Version=4.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35" resourceType="UnspecifIEd" preCondition="integratedMode" />        </handlers>    </system.webServer></configuration>

以及bindings.config:

<bindings>    <wshttpBinding>    <binding name="wshttpSecure">        <security mode="TransportWithMessageCredential">            <transport clIEntCredentialType="None" />            <message clIEntCredentialType="Username" />        </security>    </binding>    <binding name="wshttp">        <security mode="None" />    </binding>   </wshttpBinding></bindings>

Behaviors.config:

<behaviors>    <serviceBehaviors>        <behavior name="serviceBehavior">            <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />            <serviceDeBUG includeExceptionDetailinFaults="true" />            <serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="200" />            <serviceCredentials>                <usernameAuthentication usernamePasswordValIDationMode="MembershipProvIDer" membershipProvIDername="XIMembershipProvIDer"/>            </serviceCredentials>        </behavior>    </serviceBehaviors>    <!-- -->    <endpointBehaviors>        <behavior name="restBehavior">            <webhttp/>        </behavior>    </endpointBehaviors>    <!-- --></behaviors>

Service.config:

<services>    <service name="LolSoftware.MIDdleTIEr.WebService.LolWebService" behaviorConfiguration="serviceBehavior">        <endpoint name="LolWebService_WShttpEndpointSecure" contract="LolSoftware.MIDdleTIEr.Interfaces.ILolWebService" binding="wshttpBinding" bindingConfiguration="wshttpSecure"/>        <endpoint address="mex" binding="mexhttpBinding" contract="IMetadataExchange" />    </service></services>

提前致谢.

解决方法 如果要在服务上获取windowsIDentity,则必须使用windows身份验证而不是Username身份验证.请注意,windows身份验证仅适用于您域中的windows帐户.您应该更改IIS配置并禁用匿名访问.然后将wshttpBinding配置更改为:

<bindings>    <wshttpBinding>        <binding name="wshttpSecure">            <security mode="Transport">                <transport clIEntCredentialType="windows" />            </security>        </binding>   </wshttpBinding></bindings>

您不需要ASP.NET兼容性即可使用windows身份验证.

总结

以上是内存溢出为你收集整理的在我的WCF Web服务中获取WindowsIdentity全部内容,希望文章能够帮你解决在我的WCF Web服务中获取WindowsIdentity所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1067041.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存