Buscar contenidos

jueves, 7 de febrero de 2019

A sample application that shows how to call the Azure AD Graph API for an Azure AD B2C directory.




  public static async Task Login_Graph()
        {

            //https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth-ropc

            using (HttpClient client = new HttpClient())
            {
                var username = "xxxx";
                var password = "xxxx";
                var clientId = "xxxx";
                var tenandId = "xxxxx";

                var postData = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("username", username),
                    new KeyValuePair<string, string>("password", password),
                    new KeyValuePair<string, string>("grant_type","password"),
                    new KeyValuePair<string, string>("client_secret","/|s+=[0@4]t/5_c[q:8!=%!o-=?#[:|@u:##b&QLI-_Ya.B}d({Rk((}"),
                    new KeyValuePair<string, string>("scope", $"openid profile"),
                    new KeyValuePair<string, string>("client_id", clientId),
                    new KeyValuePair<string, string>("response_type","token id_token")
                });

                var response = await client.PostAsync(string.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/token", tenandId)/*TenantUrl*/, postData);

                var responseString = await response.Content.ReadAsStringAsync();

                if (response.IsSuccessStatusCode)
                {

                    var token = JsonConvert.DeserializeAnonymousType(
                        responseString,
                        new
                        {
                            access_token = string.Empty,
                            token_type = string.Empty,
                            expires_in = default(int),
                            refresh_token = string.Empty,
                            id_token = string.Empty
                        });

                    var jwt = new JwtSecurityToken(token.id_token);

                    var identity = new ClaimsIdentity(jwt.Claims, "password", "name", "role")
                    {
                        BootstrapContext = jwt
                    };

                    int contador = 1;

                    foreach (var item in identity.Claims)
                    {
                        Console.WriteLine(item.ToString());
                        contador++;
                    }

                    Console.WriteLine("TotalClaims: " + contador);

                    Console.ReadKey();
                }

            }

        }

----



How to get Azure API credentials - Client ID, Client Secret, Tenant ID and Subscription ID
https://www.youtube.com/watch?v=WygwzN9FfMQ



https://github.com/AzureADQuickStarts/B2C-GraphAPI-DotNet

A Console application for Azure AD B2C User Management the Azure AD Graph

This sample demonstrates how to perform user management by calling the Azure AD Graph in an automated fashion. This aproach is similar to a service account scenario where the application acts as itself, not as a user that signed-in via an interactive user login. This is done by using the OAuth 2.0 client credentials grant.
The application covered by this sample is a Windows command-line interface (CLI) that allows you to invoke various methods.

Steps to Run

For detailed instructions on how to run this sample, checkout this document.

Questions & Issues

Please file any questions or problems with the sample as a github issue. You can also post on Stackoverflow with the tag azure-ad-b2c.

No hay comentarios:

Publicar un comentario