从asp.net核心Credentials中找不到wcf服务

时间:2017-03-17 13:53:25

标签: asp.net wcf authentication core

我在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核心中做这个。

任何帮助:)

1 个答案:

答案 0 :(得分:0)

 CredentialCache myCredentialCache = new CredentialCache();
 myCredentialCache.Add(new Uri(url),"Basic", new NetworkCredential(userName,password,domainName));
 NetworkCredential myCredential = myCredentialCache.GetCredential(new Uri(url),"Basic");

MSDN