我在编写 MVC .net 应用程序时遇到了一些问题。我将它链接到我们的内部 AD,以便人们可以登录。但我希望他们能够使用 azure 的图形 api 重置他们的 office 365 密码。内部 AD 和 office 365 没有链接,我无法在我添加的新控制器中找到任何可以帮助我从头开始建立连接的东西。
我在控制器中的代码是:
public ActionResult ChangePassword()
{
return View();
}
[HttpPost]
public async Task<ActionResult> ChangePassword(ChangeEmailPasswordViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
var user = FindUser(User.Identity.GetUserName());
using (var client = new HttpClient())
{
var values = new Dictionary<string, string>
{
{ "password", model.NewPassword },
{ "forceChangePasswordNextLogin", "false" }
};
var content = new FormUrlEncodedContent(values);
var url = "https://graph.windows.net/162035a0-31d1-4b3c-a276-491c1dbea2f1/users/" + user.EmailAddress + "?api-version=1.6";
var response = await client.PostAsync(url, content);
var responseString = await response.Content.ReadAsStringAsync();
ViewBag.ErrorMessage = responseString;
}
return View(model);
}
#region Helpers
public UserPrincipal FindUser(string userName)
{
var principalContext = new PrincipalContext(ContextType.Domain, WebConfigurationManager.AppSettings["adInternalDomainFull"], WebConfigurationManager.AppSettings["adInternalDomain"], WebConfigurationManager.AppSettings["adInternalAdminUser"], WebConfigurationManager.AppSettings["adInternalAdminPassword"]);
var userAccount = UserPrincipal.FindByIdentity(principalContext, userName);
return userAccount;
}
#endregion