【问题标题】:Consume wcf service from asp.net core Credentials not found从 asp.net core 使用 wcf 服务 未找到凭据
【发布时间】:2017-03-17 13:53:24
【问题描述】:

我添加了一个使用 Visual Studio WCF 连接服务的服务引用。

当我尝试消费时,我收到未找到凭据的异常。

我已经能够在 ASP.NET MVC 中使用此服务,但为了完成这项工作,我做了一个技巧来覆盖请求中的标头:

public class AgendasWs2 : CatalogosAgendasWS_A
{
    protected override System.Net.WebRequest GetWebRequest(Uri uri)
    {

        HttpWebRequest request;
        request = (HttpWebRequest)base.GetWebRequest(uri);

        if (PreAuthenticate)
        {
            NetworkCredential networkCredentials = Credentials.GetCredential(uri, "Basic");
            if (networkCredentials != null)
            {

                byte[] credentialBuffer = new UTF8Encoding().GetBytes(networkCredentials.UserName + ":" + networkCredentials.Password);

                request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer);
            }
            else
            {
                throw new ApplicationException("No network credentials");
            }
        }
        return request;
    }
}

如您所见,我在发送请求之前覆盖了标头。

服务具有抢占式身份验证类型

如何在 ASP.NET Core 中做到这一点?

【问题讨论】:

    标签: asp.net-core authentication wcf


    【解决方案1】:
     CredentialCache myCredentialCache = new CredentialCache();
     myCredentialCache.Add(new Uri(url),"Basic", new NetworkCredential(userName,password,domainName));
     NetworkCredential myCredential = myCredentialCache.GetCredential(new Uri(url),"Basic");
    

    MSDN

    【讨论】:

      猜你喜欢
      • 2020-10-22
      • 2020-07-24
      • 2012-08-19
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      相关资源
      最近更新 更多