使用基本身份验证在 .NET 中使用 NAV oData REST API

Using basic authentication to consume NAV oData REST API in .NET

如标题所示,我正在尝试调用特定的 REST API - 我们称之为 cursed_api - 由第 3 方 NAV 服务器公开,要访问它,我需要提供用户名和密码。

第一点:为了通过网络浏览器访问它,出于测试目的,我确实需要使用 Internet Explorer,因为使用其他浏览器(例如 Chrome 或 Firefox)我总是会遇到相同的错误: {"error":{"code":"Authentication_InvalidCredentials","message":"The server has rejected the client credentials."}}

但这不是真正的问题。在 Visual Studio 解决方案中,我按照书本创建了一个 oData 连接服务。要下载 $metadata,唯一的预防措施是将身份验证信息(授权:基本 base 64 凭据)添加到 header。

然后我使用自动生成的代码尝试调用cursed_apiAPI,但我总是得到相同的错误信息: {"error":{"code":"Authentication_InvalidCredentials","message":"The server has rejected the client credentials."}}

我尝试在请求 header 中添加身份验证策略,方法是:

static void GetCursedApiList()
{
    string serviceRoot = String.Format("{0}/Company('{1}')", endpoint.TrimEnd('/'), company);

    NAV.NAV context = new NAV.NAV(new Uri(serviceRoot));
    context.SendingRequest2 += SendBaseAuthCredsOnTheRequest;

    IEnumerable<CursedApiObject> cursedApiObjects = context.CursedApiObject.Execute();
}

static void SendBaseAuthCredsOnTheRequest(object sender, SendingRequest2EventArgs e)
{
    string creds = String.Format(@"{0}\{1}:{2}", domain, username, password);
    creds = String.Format(@"{0}:{1}", username, password);
    string base64Creds = Convert.ToBase64String(Encoding.ASCII.GetBytes(creds));

    e.RequestMessage.SetHeader("Authorization", String.Format("Basic {0}", base64Creds));
}

然后我尝试使用上下文凭证:

static void GetCursedApiList()
{
    string serviceRoot = String.Format("{0}/Company('{1}')", endpoint.TrimEnd('/'), company);

    NAV.NAV context = new NAV.NAV(new Uri(serviceRoot));
    context.Credentials = new NetworkCredential(username, password, domain);

    IEnumerable<CursedApiObject> cursedApiObjects = context.CursedApiObject.Execute();
}

但是我运气不好。

有人遇到过同样的问题吗?

很简单,第 3 方供应商给了我无效的凭据(实际上,这是返回的错误消息)。