C# 访问Azure Graph API时出现问题

C# 访问Azure Graph API时出现问题,c#,.net,asp.net-mvc,azure,azure-ad-graph-api,C#,.net,Asp.net Mvc,Azure,Azure Ad Graph Api,我在编写一个MVC.net应用程序时遇到了一些问题。我将其链接到我们的内部广告,以便人们可以登录。但是我希望他们能够使用azure的graph api重置他们的office 365密码。内部广告和office 365没有链接,我很难在我添加的新控制器中找到任何可以帮助我从头开始建立连接的东西 控制器中的代码是: public ActionResult ChangePassword() { return View(); } [HttpPost] p

我在编写一个MVC.net应用程序时遇到了一些问题。我将其链接到我们的内部广告,以便人们可以登录。但是我希望他们能够使用azure的graph api重置他们的office 365密码。内部广告和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
public ActionResult ChangePassword()
{
返回视图();
}
[HttpPost]
公共异步任务ChangePassword(ChangeEmailPasswordViewModel模型)
{
如果(!ModelState.IsValid)
{
返回视图(模型);
}
var user=FindUser(user.Identity.GetUserName());
使用(var client=new HttpClient())
{
var值=新字典
{
{“password”,model.NewPassword},
{“forceChangePasswordNextLogin”,“false”}
};
var内容=新的FormUrlEncodedContent(值);
变量url=”https://graph.windows.net/162035a0-31d1-4b3c-a276-491c1dbea2f1/users/“+user.EmailAddress+”?api版本=1.6”;
var response=wait client.PostAsync(url、内容);
var responseString=await response.Content.ReadAsStringAsync();
ViewBag.ErrorMessage=响应字符串;
}
返回视图(模型);
}
#地区助手
公共用户主体FindUser(字符串用户名)
{
var principalContext=new principalContext(ContextType.Domain,WebConfigurationManager.AppSettings[“adInternalDomainFull”]、WebConfigurationManager.AppSettings[“adInternalDomain”]、WebConfigurationManager.AppSettings[“adInternalAdminPassword”];
var userAccount=UserPrincipal.FindByIdentity(principalContext,用户名);
返回用户帐户;
}
#端区

我不确定这段代码在做什么,但您是否尝试过使用Graph Client Library进行此操作?

我不确定这段代码在做什么,但您是否尝试过使用Graph Client Library进行此操作