简体   繁体   English

UWP:使用NTLM凭据来调用PUT Web服务

[英]UWP: Use NTLM credentials to call PUT Webservice

I'm developing an UWP-App. 我正在开发UWP-App。 Therefore I have created a PUT-Webservice with windows authentication. 因此,我创建了带有Windows身份验证的PUT-Webservice。

First I've tried to call it with this code: 首先,我尝试使用以下代码来调用它:

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;

But then I get the error: 但是然后我得到了错误:

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

Next I've tried to use "Windows.Web.Http.HttpClient": 接下来,我尝试使用“ 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"));

I've also set the needed Funcions in the Package.appxmanifest. 我还在Package.appxmanifest中设置了所需的Funcions。

I'm sorry, that I only have the screenshot in german. 抱歉,我只有德语的屏幕截图。 Here's the translation: 这是翻译:

  • Internet (Client & Server) 互联网(客户端和服务器)
  • Internet (Client) 互联网(客户端)
  • Private Networks (Client & Server) 专用网络(客户端和服务器)
  • Enterprise Authentication 企业认证

Package.appxmanifest

But then I get this Response from the webservice: 但随后我从Web服务获得以下响应: 服务器错误

Translated it means: 翻译的意思是:

401 - Unauthorized: Access denied due to invalid credentials. 401-未经授权:由于凭据无效,访问被拒绝。

The given credentials do not authorize you to view this directory or page. 给定的凭据未授权您查看此目录或页面。

Does someone have an idea what's my failure? 有人知道我的失败是什么吗?

Tank you in forward! 向前推你!

Best Regards 最好的祝福

Matthias 马蒂亚斯

Now I know a little bit more, but I don't have the solution... 现在我知道更多了,但是我没有解决方案...

I haven't said that the UWP is developed for a raspberry with Windows 10 IOT Core. 我没有说过UWP是为具有Windows 10 IOT Core的覆盆子开发的。

When I try to send the Credentials from a PC in the domain it works. 当我尝试从域中的PC发送凭据时,它可以工作。

When I try the same code on the raspberry it doesn't work. 当我在树莓派上尝试相同的代码时,它不起作用。

I think the reason is that the reaspberry is not in the domain. 我认为原因是reaspberry不在领域内。 But I can't joined Windows 10 IOT Core to a domain. 但是我无法将Windows 10 IOT Core加入域。 Only a Windows 10 IOT Enterprise can be joined to a domain. 只有Windows 10 IOT Enterprise可以加入域。 But Windows 10 IOT Enterprise can't be installed on a raspberry. 但是Windows 10 IOT Enterprise无法安装在树莓上。

So I think I have to add basic authentication to my webservice. 因此,我认为我必须在Web服务中添加基本身份验证。 If someone has an idea how I can use the credentials of an active directory user I'll be very thankfull! 如果有人知道如何使用活动目录用户的凭据,我将非常感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM