C# Azure B2C使用图形API登录
我是Azure的新手,我正在开发一个WebApi应用程序,该应用程序可以使用 图形API。我被Azure AD B2C上的身份验证用户卡住了。我通过以下代码成功地创建了用户C# Azure B2C使用图形API登录,c#,azure,authentication,asp.net-web-api,azure-ad-b2c,C#,Azure,Authentication,Asp.net Web Api,Azure Ad B2c,我是Azure的新手,我正在开发一个WebApi应用程序,该应用程序可以使用 图形API。我被Azure AD B2C上的身份验证用户卡住了。我通过以下代码成功地创建了用户 private async Task<string> CreateUserRequest(string api, string json) { AuthenticationResult result = authContext.AcquireToken(Globals.aadGraphResou
private async Task<string> CreateUserRequest(string api, string json)
{
AuthenticationResult result = authContext.AcquireToken(Globals.aadGraphResourceId, credential);
HttpClient http = new HttpClient();
string url = Globals.aadGraphEndpoint + tenant + api + "?" + Globals.aadGraphVersion;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await http.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
string error = await response.Content.ReadAsStringAsync();
object formatted = JsonConvert.DeserializeObject(error);
throw new WebException("Error Calling the Graph API: \n" + JsonConvert.SerializeObject(formatted, Formatting.Indented));
}
return await response.Content.ReadAsStringAsync();
}
私有异步任务CreateUserRequest(字符串api、字符串json)
{
AuthenticationResult=authContext.AcquireToken(Globals.aadGraphResourceId,凭证);
HttpClient http=新的HttpClient();
字符串url=Globals.aadGraphEndpoint+tenant+api+“?”+Globals.aadGraphVersion;
HttpRequestMessage请求=新的HttpRequestMessage(HttpMethod.Post,url);
request.Headers.Authorization=新的AuthenticationHeaderValue(“承载者”,result.AccessToken);
request.Content=newstringcontent(json,Encoding.UTF8,“application/json”);
HttpResponseMessage response=等待http.SendAsync(请求);
如果(!response.issucessStatusCode)
{
字符串错误=wait response.Content.ReadAsStringAsync();
对象格式化=JsonConvert.DeserializeObject(错误);
抛出新的WebException(“调用图形API时出错:\n”+JsonConvert.SerializeObject(格式化、格式化、缩进));
}
return wait response.Content.ReadAsStringAsync();
}
现在,我需要使用与以下代码相同的方法对用户进行身份验证并获取令牌
现在,我需要使用api调用登录/登录,将用户名和密码作为参数传递给上述方法
public async Task<string> B2CAuthenticateUser(string userName, string password)
{
return await SendGraphValidateUser("/users/" + userName, null);
}
public async Task<string> SendGraphValidateUser(string api, string query)
{
AuthenticationResult result = authContext.AcquireToken("https://graph.windows.net", credential);
HttpClient http = new HttpClient();
**Here I need your help**
> // string url = "https://graph.windows.net/" + tenant + api + "?" +
> Globals.aadGraphVersion;
> // if (!string.IsNullOrEmpty(query))
> // {
> // url += "&" + query;
> // }
>
>
> //HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
HttpResponseMessage response = await http.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
string error = await response.Content.ReadAsStringAsync();
object formatted = JsonConvert.DeserializeObject(error);
throw new WebException("Error Calling the Graph API: \n" + JsonConvert.SerializeObject(formatted, Formatting.Indented));
}
return await response.Content.ReadAsStringAsync();
}
公共异步任务B2CAuthenticateUser(字符串用户名、字符串密码)
{
返回wait wait SendGraphValidateUser(“/users/”+用户名,null);
}
公共异步任务SendGraphValidateUser(字符串api,字符串查询)
{
AuthenticationResult=authContext.AcquireToken(“https://graph.windows.net“,凭证);
HttpClient http=新的HttpClient();
**我需要你的帮助**
>//字符串url=”https://graph.windows.net/“+租户+api+”?”+
>Globals.aadGraphVersion;
>//如果(!string.IsNullOrEmpty(查询))
> // {
>//url+=“&”+查询;
> // }
>
>
>//HttpRequestMessage请求=新的HttpRequestMessage(HttpMethod.Get,url);
request.Headers.Authorization=新的AuthenticationHeaderValue(“承载者”,result.AccessToken);
HttpResponseMessage response=等待http.SendAsync(请求);
如果(!response.issucessStatusCode)
{
字符串错误=wait response.Content.ReadAsStringAsync();
对象格式化=JsonConvert.DeserializeObject(错误);
抛出新的WebException(“调用图形API时出错:\n”+JsonConvert.SerializeObject(格式化、格式化、缩进));
}
return wait response.Content.ReadAsStringAsync();
}
我希望有办法做到这一点。如果你能建议一种方法,我将不胜感激
附:我已经回答了以下相关问题,没有多大帮助]
最好使用您自己的应用程序验证用户凭据的身份验证工具对用户进行验证。谢谢@Chris,但文档中提到的帖子URL不正确。我刚刚尝试了一个非常简单的例子,但由于文档发布的URL,它失败了。它需要更新以包含以下URL: