试图获取使用WCF客户端的Windows用户名 [英] Trying to obtain windows username of the client using WCF

查看:245
本文介绍了试图获取使用WCF客户端的Windows用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让谁正在访问,正在我的本地IIS托管ASP.NET页面的客户机的Windows用户名。我callign返回客户端的Windows用户名在ASP.NET页内的WCF服务。我碰到那么多帖子就来了,其中大部分都提示


  1. OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name

  2. HttpContext.Current.User.Identity.Name

  3. HttpContext.Current.User.Identity.Name

应该工作。我所面临的问题是1总是返回null。 2和3总是给我回本地用户名,而不是请求用户的名称。我失去了在ASP.NET和WCF服务的web.configs什么。

IIS属性:集成Windows身份验证启用

下面是code。

WCF

 公共字符串GetWindowsUser()
{
   字符串TEMP = OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name;
   字符串temp1中= HttpContext.Current.User.Identity.Name;
   字符串TEMP2 = HttpContext.Current.User.Identity.Name;
   回归温度:+温度+\\ nTemp1:+ temp1目录+\\ nTemp2:+ TEMP2;
}

的web.config

 <&的System.Web GT;
    <编译调试=假targetFramework =4.0/>
< /system.web>
< system.serviceModel>
    <&绑定GT;
        <&basicHttpBinding的GT;
            <绑定名称=BasicHttpBinding_IService1closeTimeout =00:01:00openTimeout =00:01:00receiveTimeout =00:10:00的SendTimeout =00:01:00allowCookies =false的bypassProxyOnLocal =假hostNameComparisonMode =StrongWildcardMAX​​BUFFERSIZE =65536maxBufferPoolSize =524288maxReceivedMessageSize =65536messageEncoding =文本textEncoding =UTF-8transferMode =缓冲useDefaultWebProxy =真>
                < readerQuotas MAXDEPTH =32maxStringContentLength =8192maxArrayLength =16384maxBytesPerRead =4096maxNameTableCharCount =16384/>
                <安全模式=无>
                    <运输clientCredentialType =无proxyCredentialType =无的境界=/>
                    <消息clientCredentialType =用户名algorithmSuite =默认/>
                < /安全>
            < /&结合GT;
        < / basicHttpBinding的>
    < /绑定>
    <客户端>
        <端点地址=HTTP://本地主机:4772 / Services.svc绑定=basicHttpBinding的bindingConfiguration =BasicHttpBinding_IService1合同=WindowsAuthServices.IService1NAME =BasicHttpBinding_IService1/>
    < /客户>
< /system.serviceModel>

ASP.NET页:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    WindowsAuthServices.Service1Client客户端=新WindowsAuthServices.Service1Client();
    lblWelcome.Text = client.GetWindowsUser();
}

Web.Config中

 <&的System.Web GT;
    <编译调试=真targetFramework =4.0/>
< /system.web>
< system.serviceModel>
    <&绑定GT;
        <&basicHttpBinding的GT;
            <绑定名称=BasicHttpBinding_IService1closeTimeout =00:01:00openTimeout =00:01:00receiveTimeout =00:10:00的SendTimeout =00:01:00allowCookies =false的bypassProxyOnLocal =假hostNameComparisonMode =StrongWildcardMAX​​BUFFERSIZE =65536maxBufferPoolSize =524288maxReceivedMessageSize =65536messageEncoding =文本textEncoding =UTF-8transferMode =缓冲useDefaultWebProxy =真>
                < readerQuotas MAXDEPTH =32maxStringContentLength =8192maxArrayLength =16384maxBytesPerRead =4096maxNameTableCharCount =16384/>
                <安全模式=无>
                    <运输clientCredentialType =无proxyCredentialType =无的境界=/>
                    <消息clientCredentialType =用户名algorithmSuite =默认/>
                < /安全>
            < /&结合GT;
        < / basicHttpBinding的>
    < /绑定>
    <客户端>
        <端点地址=HTTP://本地主机:4772 / Services.svc绑定=basicHttpBinding的bindingConfiguration =BasicHttpBinding_IService1合同=WindowsAuthServices.IService1NAME =BasicHttpBinding_IService1/>
    < /客户>
< /system.serviceModel>


解决方案

我终于找到了解决办法。当其他用户试图访问使用IP我的本地IIS,我的本地IIS假定它是一个互联网的要求,而不是内部网络请求,因为它的Windows身份验证,它仅适用于Intranet请求。为了解决这个问题,我不得不主办我的网站上我们的域名服务器之一。由于域服务器已经设置,使得仅在该域中的用户可以访问它,现在它有了Windows登录信息。并有结束我的痛苦。

I am trying to get the Windows User name of the client who is accessing the ASP.NET page that is being hosted on my Local IIS. I am callign a WCF service within the ASP.NET page which returns the windows username of the client. I came across so many posts regarding and most of them are suggesting that

  1. OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name
  2. HttpContext.Current.User.Identity.Name
  3. HttpContext.Current.User.Identity.Name

should work. The problems I am facing is "1" is always returning Null. "2" and "3" are always returning my local user name and not the requesting user's name. Am I missing anything in the web.configs of both ASP.NET and WCF service.

IIS properties: Integrated Windows authentication Enabled.

Here is the code.

WCF

public string GetWindowsUser()    
{
   string temp = OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name;
   string temp1 = HttpContext.Current.User.Identity.Name;
   string temp2 = HttpContext.Current.User.Identity.Name;
   return "Temp: "+temp+" \nTemp1: "+temp1+" \nTemp2: "+temp2;
}

WEB.Config

<system.web>
    <compilation debug="false" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
                    <message clientCredentialType="UserName" algorithmSuite="Default"/>
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:4772/Services.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" contract="WindowsAuthServices.IService1" name="BasicHttpBinding_IService1"/>
    </client>
</system.serviceModel>

ASP.NET Page:

protected void Page_Load(object sender, EventArgs e)
{
    WindowsAuthServices.Service1Client client = new WindowsAuthServices.Service1Client();
    lblWelcome.Text = client.GetWindowsUser();
}

Web.Config

<system.web>
    <compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
                    <message clientCredentialType="UserName" algorithmSuite="Default"/>
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:4772/Services.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" contract="WindowsAuthServices.IService1" name="BasicHttpBinding_IService1"/>
    </client>
</system.serviceModel>

解决方案

I finally found the solution. When the other user are trying to access my local IIS using IP, my local IIS assumes that it is an internet request and not intranet request and since its windows authentication, it works only with intranet requests. To solve this I had to host my website on one of our domain servers. Since the domain server was already setup such that only users in that domain can access it, now it has the windows login info. And there ends my misery.

这篇关于试图获取使用WCF客户端的Windows用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆