【问题标题】:UWP: Use NTLM credentials to call PUT WebserviceUWP:使用 NTLM 凭据调用 PUT Webservice
【发布时间】:2018-10-24 17:35:38
【问题描述】:

我正在开发一个 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 中设置了所需的功能。

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

  • 互联网(客户端和服务器)
  • 互联网(客户端)
  • 专用网络(客户端和服务器)
  • 企业认证

但是我从网络服务中得到了这个响应:

翻译过来的意思是:

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

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

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

助你前进!

最好的问候

马蒂亚斯

【问题讨论】:

  • 如果你直接在fiddler中模拟这些操作,你成功了吗?

标签: c# uwp authorization httpclient windows-iot-core-10


【解决方案1】:

现在我知道了一点,但我没有解决办法......

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

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

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

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

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

【讨论】:

  • 是的,对于Windows Authentication,客户端和服务应该在同一个域中。对于不在同一个域中使用 AD 进行身份验证,您可以尝试基本身份验证。该服务将通过 AD 验证用户凭据,而不检查它是否在同一个域中。您需要提供正确的用户凭据。
猜你喜欢
  • 1970-01-01
  • 2014-03-12
  • 2020-04-30
  • 2013-08-03
  • 2013-08-15
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多