Users are not always created with GraphClient in Azure Active Directory in ASP.NET

0

I have this strange problem with GraphClient, sometimes users are created, sometimes not, and when I add a user (from a list iterating and calling the method that users add) if it works, most of the time. It does not give any error, the same thing is repeated putting in production the web application, or in console, in debug or release, when debugging step by step all the catch is executed without problems or warnings, and here the code:

* Some credentials were removed for security.

This is the format in which the data is received in the action:

 List<Usuarios> Usuarios = new List<Usuario>();
 //CreateUser();
 Usuario usuario = new Usuario();
        usuario.Nombre = "Julian F";
        usuario.Usuario= "[email protected]";
        usuario.EmpleadoID= "E212Julian";
        usuario.Pass = "!Aa12345";
        Usuario.Add(usuario);

Code in action:

  public string Add_usuarios(IList<Usuarios> usuarios)
{
foreach (var i in usuarios)
            {
                //Usuario a Azure AD
                 CrearUsuario_AD(i.Nombre.Trim(), i.Usuario.Trim(), i.EmpleadoID.Trim(), i.Pass.Trim());//1
            }
}

Methods to add users:

 // This is the URL the application will authenticate at.
    const string authString = "https://login.windows.net/xxxx-xxxx-xxxxx";

    // These are the credentials the application will present during authentication
    // and were retrieved from the Azure Management Portal.
    // *** Don't even try to use these - they have been deleted.
    const string clientID = "xxx-xxx-xxxxx-xxx-x-xx";
    const string clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxx=";

    // The Azure AD Graph API is the "resource" we're going to request access to.
    const string resAzureGraphAPI = "https://graph.windows.net";

    // The Azure AD Graph API for my directory is available at this URL.
    const string serviceRootURL = "https://graph.windows.net/xxxxxxxxxx-xxxxxxx-xxxxxxx";

    private static ActiveDirectoryClient GetActiveDirectoryClient()
    {
        Uri serviceRoot = new Uri(serviceRootURL);
        ActiveDirectoryClient adClient = new ActiveDirectoryClient(
            serviceRoot,
            async () => await GetAppTokenAsync()
            );//3
        return adClient;//4
    }
    private static async Task<string> GetAppTokenAsync()
    {
        AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
        ClientCredential clientCred = new ClientCredential(clientID, clientSecret);//8
        AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(resAzureGraphAPI, clientCred);//9
        return authenticationResult.AccessToken;
    }
    private static void CrearUsuario_AD(string Nombre, string usuario, string EmpleadoID, string Contraseña)
    {
        var adClient = GetActiveDirectoryClient();//2

        //Construccion de user
        //string userEmail = "[email protected]";
        string mailNickname = usuario.Split(new char[] { '@' }).FirstOrDefault();
        var userGraphObj = new User()
        {
            GivenName = Nombre,//nombre (Nombre)
                                 //Surname = "aSur",
                                 // Mobile = "0123456789",
            MailNickname = mailNickname,//nombre de usuario (Usuario)
            DisplayName =EmpleadoID,//nombre a mostrar (Empleado ID)
            AccountEnabled = true
        };//5
        //userGraphObj.OtherMails.Add(userEmail);//correo alternativo
        string tenantName = "midominio.com";
        //var userPrincipleName = "test_" + Guid.NewGuid().ToString() + "@" + tenantName;//6
        userGraphObj.UserPrincipalName = mailNickname + "@" + tenantName;/* userPrincipleName;*/

        var tempPassword = Contraseña; //(Contraseña)
        var passwordProfile = new PasswordProfile
        {
            Password = tempPassword,
            ForceChangePasswordNextLogin = true
        };
        userGraphObj.PasswordProfile = passwordProfile;

        //lo de abajo pero con .Wait();
        adClient.Users.AddUserAsync(userGraphObj);//7
    }
    
asked by Alex Lz 03.08.2018 в 22:07
source

0 answers