首页 > 解决方案 > UWP:使用 NTLM 凭据调用 PUT Webservice

问题描述

我正在开发一个 UWP 应用程序。因此,我创建了一个带有 Windows 身份验证的 PUT-Webservice。

首先,我尝试使用以下代码调用它:

JPasswordChangeData jPasswordChangeData = new JPasswordChangeData()
{
    OldPassword = oldpassword,
    NewPassword = newpassword
};

var credential = new NetworkCredential("<username>", "<password>", "<domain>");

string apiServerSecurePath = "https://MyServername:6501/";
var myCache = new CredentialCache();

// Add the target Uri to the CredentialCache with credential object
myCache.Add(new Uri(apiServerSecurePath), "NTLM", executingCredentials);

// Create an HttpClientHandler to add some settings
var handler = new HttpClientHandler();
handler.AllowAutoRedirect = true;
handler.Credentials = myCache;

HttpClient httpClient = new HttpClient(handler);

HttpResponseMessage httpResponseMessage = httpClient
    .PutAsync(apiServerSecurePath + "api/ActiveDirectory/ChangePassword/" + username,
    new StringContent(JsonConvert.SerializeObject(jPasswordChangeData), Encoding.UTF8, "application/json")).Result;

但后来我得到了错误:

The value 'System.Net.CredentialCache' is not supported for property 'Credentials'.

接下来我尝试使用“Windows.Web.Http.HttpClient”:

var filter = new HttpBaseProtocolFilter();
filter.AllowAutoRedirect = true;
filter.ServerCredential = new PasswordCredential("<Domain>", "<username>", "<password>");
filter.AllowUI = false;

JPasswordChangeData jPasswordChangeData = new JPasswordChangeData()
{
    OldPassword = oldpassword,
    NewPassword = newpassword
};

Windows.Web.Http.HttpClient windowsHttpClient = new Windows.Web.Http.HttpClient(filter);

//HttpClient httpClient = new HttpClient();
var httpResponseMessage = await windowsHttpClient.PutAsync(
new Uri("http://MyDomain:6001/api/ActiveDirectory/ChangePassword/" + txtblk_samaccountname.Text.Trim()),
new HttpStringContent(JsonConvert.SerializeObject(jPasswordChangeData), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json"));

我还在 Package.appxmanifest 中设置了所需的功能。

对不起,我只有德语的截图。这是翻译:

包.appxmanifest

但后来我从网络服务得到这个响应: 服务器错误

翻译过来的意思是:

401 - 未经授权:由于凭据无效,访问被拒绝。

给定的凭据不授权您查看此目录或页面。

有人知道我的失败是什么吗?

让你向前冲!

此致

马蒂亚斯

标签: c#uwpauthorizationhttpclientwindows-iot-core-10

解决方案


现在我知道更多一点,但我没有解决方案......

我没有说 UWP 是为带有 Windows 10 IOT Core 的树莓派开发的。

当我尝试从域中的 PC 发送凭据时,它可以工作。

当我在覆盆子上尝试相同的代码时,它不起作用。

我认为原因是 reaspberry 不在域中。但我无法将 Windows 10 IOT Core 加入域。只有 Windows 10 IOT Enterprise 可以加入域。但是 Windows 10 IOT Enterprise 不能安装在树莓派上。

所以我想我必须向我的网络服务添加基本身份验证。如果有人知道如何使用 Active Directory 用户的凭据,我将不胜感激!


推荐阅读