【问题标题】:Authentication scheme in .net core calling wcf.net 核心中调用 wcf 的身份验证方案
【发布时间】:2020-12-07 18:53:32
【问题描述】:

我有一个 .net core 2.1 web api,它通过 windows 身份验证调用 WCF 服务:

appsettings.json

"MyService": {
    "BasicHttpBinding": {
            "Security": {
                "Mode": "TransportCredentialOnly",
                "Transport": {
                    "ClientCredentialType": "Windows"
                }
            }
        },
        "UserPrincipalName": "mydomain/myUser"
},

mydomain/myUser - 它是一个有权调用 MyService 和 MyMethod() 的用户

code.cs

        using (var client = new ServiceClient<IMyService>(BasicHttpBinding, new EndpointAddress(new Uri(myServiceUrl), UpnEndpointIdentity)))
        {
            var result = client.Proxy.MyMethod();
            return result;
        }

当我尝试从我的应用程序调用 wcf 服务时,出现错误:

HTTP 请求未经客户端身份验证方案授权 '谈判'。从服务器收到的身份验证标头是“协商”

我已尝试在 startup.cs 中指明身份验证架构:

services.AddAuthentication(IISDefaults.Negotiate);

但运气不好,异常还是被复制了。

我该如何解决这个问题?

【问题讨论】:

    标签: c# wcf authentication asp.net-core-2.1


    【解决方案1】:

    如果使用windows认证,客户端调用服务器时需要提供windows凭据:

     Service1Client service1Client = new Service1Client();
     service1Client.ClientCredentials.Windows.ClientCredential.UserName = "Administrator";
     service1Client.ClientCredentials.Windows.ClientCredential.Password = "Password";
    

    如果客户端没有提供windows凭据,会报错:

    如果问题仍然存在,请随时告诉我。

    更新

    尝试使用WCF Web Service Reference添加服务引用来调用服务:

     ServiceReference1.Service1Client service1Client = new ServiceReference1.Service1Client();
    

    我给大家介绍一下我的项目,部署在 IIS 中:

    <?xml version="1.0"?>
    <configuration>
    
      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.7.2" />
        <httpRuntime targetFramework="4.7.2"/>
      </system.web>
        <system.serviceModel>
            <behaviors>
                <serviceBehaviors>
                    <behavior name="serviceBehaviour">
                        <serviceMetadata httpGetEnabled="true"  httpsGetEnabled="true"/>
                        <serviceDebug includeExceptionDetailInFaults="false" />
                    </behavior>
                </serviceBehaviors>
            </behaviors>
            <bindings>
                <basicHttpBinding>
                    <binding name="HttpBinding_IService">
                        <readerQuotas maxDepth="32" maxStringContentLength="1000000" maxArrayLength="163840000" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                        <security mode="Transport">
                            <transport clientCredentialType="Windows" proxyCredentialType="None"/>
                        </security>
                    </binding>
                </basicHttpBinding>
            </bindings>
            <services>
                <service name="WcfService1.Service1" behaviorConfiguration="serviceBehaviour">
                    <endpoint address="Service" binding="basicHttpBinding" bindingConfiguration="HttpBinding_IService" contract="WcfService1.IService1" />
                </service>
            </services>
        </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
          -->
        <directoryBrowse enabled="true"/>
      </system.webServer>
    
    </configuration>
    

    这是 web.config。

    在IIS中,我为WCF添加了两个绑定,一个是http,一个是https,使用https需要证书:

    我们还需要关闭匿名认证,开启windows认证:

    如果Mode设置为Transport,我们还需要添加htts绑定和证书。

    您可以尝试以上步骤重新部署,希望您能成功。

    【讨论】:

    • 看起来合乎逻辑,但不幸的是它没有帮助。问题依旧重现client.ClientCredentials.Windows.ClientCredential.UserName = "myUser";client.ClientCredentials.Windows.ClientCredential.Password = "myPassword";client.ClientCredentials.Windows.ClientCredential.Domain = "myDomain";
    • 是不是因为您提供了错误的 Windows 凭据?
    • 需要提供服务器端windows凭据。
    • 凭证有效,我输入了自己的凭证。不幸的是,我不能提供错误的截图,因为这个问题只能在测试环境中重现(在本地它工作正常)。日志截图:gyazo.com/9a31370146c72adeba5bb6d221cfff97
    • 您尝试使用 WCF Web 服务引用添加服务引用来调用服务。
    猜你喜欢
    • 1970-01-01
    • 2019-11-03
    • 2019-02-23
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多