【发布时间】: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