简体   繁体   中英

Azure B2C sign in using Graph API

I am new to Azure and i am working on a WebApi app which can create/delete/update and authenticate a user on Azure AD B2C tenant using Graph API. I am stuck with the authenticate user on Azure AD B2C. I was successfully able to create user by following code.

  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();
}

and now i need to use the same way for Authenticate user and get token as the for following code

and now i need to log in/sign in using api call by passing username and password as parameters slimier to above way.

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();
} 

I hope there should be a way to do this. appreciate if you can suggest a way to do it

PS : I have gone through with following related questions and wasn't help much] Authenticate a user on Azure AD B2C using Graph API

最好使用资源所有者策略对用户进行身份验证, 该策略使您自己的应用程序能够验证用户的凭据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM