locked
Unable to add user in Windows Azure active directory using WebAPI RRS feed

  • Question

  • User990493146 posted

     am facing one issue regarding windows azure active directory.Actually I am trying to add user to azure active directory using webAPI. While I am running WebAPI 's particular method for adding user to active directory ..i am not getting any error but not getting any response from windows azure active directory as well as user is not adding in active directory.

    Same thing while i am using console application,from console application itself i can add user to windows active directory. Please help on this.

    I am providing my code for reference purpose.

    it is helper class for generating oauth token..

    public class AuthenticationHelper { public static string TokenForUser; /// <summary> /// Async task to acquire token for Application. /// </summary> /// <returns>Async Token for application.</returns> public static async Task<string> AcquireTokenAsyncForApplication() { return GetTokenForApplication(); }

    /// <summary> /// Get Token for Application. /// </summary> /// <returns>Token for application.</returns> public static string GetTokenForApplication() { try { AuthenticationContext authenticationContext = new AuthenticationContext(GraphApiConstants.AuthString, false); // Config for OAuth client credentials ClientCredential clientCred = new ClientCredential(GraphApiConstants.ClientId, GraphApiConstants.ClientSecret); AuthenticationResult authenticationResult = authenticationContext.AcquireToken(GraphApiConstants.ResourceUrl, clientCred); string token = authenticationResult.AccessToken; return token; } catch(Exception ex) { throw ex; }
    }

    /// <summary> /// Get Active Directory Client for Application. /// </summary> /// <returns>ActiveDirectoryClient for Application.</returns> public static ActiveDirectoryClient GetActiveDirectoryClientAsApplication() { Uri servicePointUri = new Uri(GraphApiConstants.ResourceUrl); Uri serviceRoot = new Uri(servicePointUri, GraphApiConstants.TenantId); ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot, async () => await AcquireTokenAsyncForApplication()); return activeDirectoryClient; }

    /// <summary> /// Async task to acquire token for User. /// </summary> /// <returns>Token for user.</returns> public static async Task<string> AcquireTokenAsyncForUser() { return GetTokenForUser(); }

    /// <summary> /// Get Token for User. /// </summary> /// <returns>Token for user.</returns> public static string GetTokenForUser() { try { if (TokenForUser == null) { //var redirectUri = new Uri("https://localhost/ShellTripApp.com"); var redirectUri = new Uri("https://localhost"); AuthenticationContext authenticationContext = new AuthenticationContext(GraphApiConstants.AuthString, false); AuthenticationResult userAuthnResult = authenticationContext.AcquireToken(GraphApiConstants.ResourceUrl, GraphApiConstants.ClientIdForUserAuthn, redirectUri, PromptBehavior.Always); TokenForUser = userAuthnResult.AccessToken;

    } } catch(Exception ex) { throw ex; }

    return TokenForUser; }

    /// <summary> /// Get Active Directory Client for User. /// </summary> /// <returns>ActiveDirectoryClient for User.</returns> public static ActiveDirectoryClient GetActiveDirectoryClientAsUser() { Uri servicePointUri = new Uri(GraphApiConstants.ResourceUrl); Uri serviceRoot = new Uri(servicePointUri, GraphApiConstants.TenantId); ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot, async () => await AcquireTokenAsyncForUser()); return activeDirectoryClient; } }

    --Controller code

    public TRIPResponse AddTripUser(UserADModel Model) { string msg = string.Empty; ClsUserProfileDetails objClsUserProfileDetails = null; UserProfileDetailsImp objUserProfileDetailsImp=null; TRIPResponse response = null; try { if(Model!=null) { ActiveDirectoryClient activeDirectoryClient; activeDirectoryClient = AuthenticationHelper.GetActiveDirectoryClientAsApplication();

    VerifiedDomain initialDomain = new VerifiedDomain(); VerifiedDomain defaultDomain = new VerifiedDomain(); ITenantDetail tenant = null;

    List<ITenantDetail> tenantsList = activeDirectoryClient.TenantDetails .Where(tenantDetail => tenantDetail.ObjectId.Equals(GraphApiConstants.TenantId)) .ExecuteAsync().Result.CurrentPage.ToList();

    if (tenantsList.Count > 0) { tenant = tenantsList.First(); }

    TenantDetail tenantDetailInfo = (TenantDetail)tenant;

    initialDomain = tenantDetailInfo.VerifiedDomains.First(x => x.Initial.HasValue && x.Initial.Value); defaultDomain = tenantDetailInfo.VerifiedDomains.First(x => x.@default.HasValue && x.@default.Value);

    IUser newUser = new User();

    if (defaultDomain.Name != null) { newUser.DisplayName = Model.LastName + "," + Model.FirstName; newUser.UserPrincipalName = Model.Email; newUser.Surname = Model.LastName; newUser.MailNickname = Model.FirstName; newUser.AccountEnabled = true; newUser.PasswordProfile = new PasswordProfile { Password = "TempP@ssw0rd!", ForceChangePasswordNextLogin = false, };

    } activeDirectoryClient.Users.AddUserAsync(newUser).Wait();

    objClsUserProfileDetails = new ClsUserProfileDetails(); objClsUserProfileDetails.FirstName = Model.FirstName; objClsUserProfileDetails.LastName = Model.LastName; objClsUserProfileDetails.DisplayName = Model.LastName + "," + Model.FirstName; objClsUserProfileDetails.UserRoleCode = Model.UserRoleCode; objClsUserProfileDetails.Email = Model.Email; objClsUserProfileDetails.CreationDate = DateTime.UtcNow; objClsUserProfileDetails.CreationBy = Model.Createdby; if (Model.UserRoleCode.Contains("R")) objClsUserProfileDetails.HSSEReviewer = "Y"; else objClsUserProfileDetails.HSSEReviewer = "N"; objClsUserProfileDetails.SyncStatus = "S"; objClsUserProfileDetails.RecordStatus = "1"; objUserProfileDetailsImp = new UserProfileDetailsImp(); msg=objUserProfileDetailsImp.InsertUserProfileDetails(objClsUserProfileDetails); response = new TRIPResponse(ResponseStatus.Success, msg);

    }

    } catch (Exception ex) { response = new TRIPResponse("Error in AddTripUser", Utilities.GetInnermostException(ex)); }

    finally { objUserProfileDetailsImp = null; objClsUserProfileDetails = null; } return response; }

    Friday, May 6, 2016 10:26 AM

Answers

  • User-646145796 posted

    Hi,

     

    i am not getting any error but not getting any response from windows azure active directory as well as user is not adding in active directory.

    Please try to use fiddler to check the response, and provide detailed error message here to better understand your issue. If you want to add user in windows Azure active directory, I would suggest you try AD sample at: https://azure.microsoft.com/en-us/documentation/articles/active-directory-code-samples/. such as if use C#, Web App, you can try this sample code:WebApp-GraphAPI-DotNet. It demonstrator us how to use graph api to access Azure AD directory data.

    Best Regards,

    Jambor

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 9, 2016 3:08 AM