简体   繁体   English

使用图形 API 登录 Azure B2C

[英]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.我是 Azure 的新手,我正在开发一个 WebApi 应用程序,该应用程序可以使用 Graph API 在 Azure AD B2C 租户上创建/删除/更新和验证用户。 I am stuck with the authenticate user on Azure AD B2C.我坚持使用 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现在我需要对 Authenticate user 使用相同的方式并获取令牌作为以下代码

and now i need to log in/sign in using api call by passing username and password as parameters slimier to above way.现在我需要通过将用户名和密码作为参数传递给上述方式来使用 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();
} 

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 PS:我已经解决了以下相关问题,但没有多大帮助] 使用 Graph API 对 Azure AD B2C 上的用户进行身份验证

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure B2C 自定义属性图 API - Azure B2C Custom Attribute Graph API Azure B2C - 使用 Graph API 注册用户,而不是使用用户流/自定义策略 - Azure B2C - using Graph API to register users instead of using user flows / custom policies 使用http post请求使用Graph API在Azure Active Directory(B2C)中创建新用户 - Create a new user in Azure Active Directory (B2C) with Graph API, using http post request 使用用户分配的托管标识访问 Azure AD B2C 和 MS Graph API - Access Azure AD B2C with MS Graph API using User-Assigned Managed Identity 如何在不使用Microsoft页面的情况下在Azure AD B2C中使用graph api进行社交登录? - How to use graph api for social login in Azure AD B2C without using Microsoft page? 使用 B2C 图形 API 获取用户的 Azure 目录角色 - Get Azure directory role of the user using B2C graph API 带有 B2C AD 的 Azure Graph - Azure Graph with B2C AD 如何以编程方式通过 Microsoft Graph 获取 Azure AD b2c 登录日志 - how to get Azure AD b2c sign-in logs through Microsoft Graph programatically 使用 Azure AD B2C 使用个人用户帐户登录 ASP.Net Core Web 应用程序 -&gt; API - Sign in to ASP.Net Core Web Application -> API with Individual User Accounts using Azure AD B2C B2C Graph API 更新用户、UserPrincipalName 作为电子邮件:URL 不支持加号 (+) 登录电子邮件? - B2C Graph API Update User, UserPrincipalName as an email: URL does not support plus (+) sign in email?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM