c# - 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 中设置了所需的功能。
对不起,我只有德语的截图。这是翻译:
- 互联网(客户端和服务器)
- 互联网(客户端)
- 专用网络(客户端和服务器)
- 企业认证
翻译过来的意思是:
401 - 未经授权:由于凭据无效,访问被拒绝。
给定的凭据不授权您查看此目录或页面。
有人知道我的失败是什么吗?
让你向前冲!
此致
马蒂亚斯
解决方案
现在我知道更多一点,但我没有解决方案......
我没有说 UWP 是为带有 Windows 10 IOT Core 的树莓派开发的。
当我尝试从域中的 PC 发送凭据时,它可以工作。
当我在覆盆子上尝试相同的代码时,它不起作用。
我认为原因是 reaspberry 不在域中。但我无法将 Windows 10 IOT Core 加入域。只有 Windows 10 IOT Enterprise 可以加入域。但是 Windows 10 IOT Enterprise 不能安装在树莓派上。
所以我想我必须向我的网络服务添加基本身份验证。如果有人知道如何使用 Active Directory 用户的凭据,我将不胜感激!
推荐阅读
- android - 在 Kotlin 中使用 Recycler 视图显示 Cloud Firestore 中的数据
- javascript - Eslint react/jsx-one-expression-per-line:允许变量和 JSX 字符串在同一行,但不允许元素
- go - 尝试使用指针字段初始化结构时出现 nil 指针取消引用错误
- apache-spark - 如何提取数据集内容 n 行 n 行?
- r - 从 emacs 键绑定关闭 R 绘图窗口
- java - 春季启动+码头工人+ kubernetes
- c++ - ZeroMQ PubSub 使用 inproc 套接字永远挂起
- swift - 在 iOS Swift 中流式传输 Spotify preview_url
- pm2 - PM2 1个月后自动删除进程~
- database - Laravel:选择条件在另一个表中的所有电影