从Xamarin.Android应用程序添加用户(Azure Active Directory)

时间:2017-04-12 17:57:31

标签: azure active-directory xamarin.android

您好我正在尝试使用GRAPH API将新用户添加到Azure中的Active Directory应用程序但我不知道为了使用C#构建客户端所需的一些URL,我确定的唯一字符串是clientSecret 。 有人可以帮忙吗?

 const string authString = "";
    const string clientID = "";
    const string clientSecret = "";
    const string resAzureGraphAPI = "";
    const string serviceRootURL = "";
    static Uri serviceRoot = new Uri(serviceRootURL);
   ActiveDirectoryClient adClient = new ActiveDirectoryClient(
        serviceRoot,
        async () => await GetAppTokenAsync());
     private  void But_Click(object sender, EventArgs e)
    {
        // Create a new user object.
        var newUser = new User()
        {
            // Required settings
            DisplayName = "Jay Hamlin",
            UserPrincipalName = "jayhamlin@cloudalloc.com",
            PasswordProfile = new PasswordProfile()
            {
                Password = "H@ckMeNow!",
                ForceChangePasswordNextLogin = false
            },
            MailNickname = "JayHamlin",
            AccountEnabled = true,

            // Some (not all) optional settings
            GivenName = "Jay",
            Surname = "Hamlin",
            JobTitle = "Programmer",
            Department = "Development",
            City = "Dallas",
            State = "TX",
            Mobile = "214-123-1234",
        };

        // Add the user to the directory
        adClient.Users.AddUserAsync(newUser).Wait();
    }`
    private static async Task<string> GetAppTokenAsync()
    {
        // Instantiate an AuthenticationContext for my directory (see authString above).
        AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);

        // Create a ClientCredential that will be used for authentication.
        // This is where the Client ID and Key/Secret from the Azure Management Portal is used.
        ClientCredential clientCred = new ClientCredential(clientID, clientSecret);

        // Acquire an access token from Azure AD to access the Azure AD Graph (the resource)
        // using the Client ID and Key/Secret as credentials.
        AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(resAzureGraphAPI, clientCred);

        // Return the access token.
        return authenticationResult.AccessToken;
    }`

2 个答案:

答案 0 :(得分:0)

this blog post中,您可以找到您正在寻找的字符串的详细信息:

  1. authString
    • authority将返回token。这通常是第三方端点,它将为您处理OAuth身份验证。一个例子是:https://login.windows.net/common/oauth2/authorize
  2. clientID
    • 可以从Azure应用程序门户
    • 获取
  3. resAzureGraphAPI
    • 图表API的Uri。最有可能是https://graph.windows.net
  4. serviceRootURL
    • 应用程序域的Uri。您应该可以在Azure Active Directory的“域”选项卡下找到它。
  5. 此外,您应该避免在.Wait();上调用AddUserAsync(),因为它会阻止执行。相反,您应该await结果。

答案 1 :(得分:0)

请先阅读Azure Active Directory developer's guide。另外,使用时:

  AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(resAzureGraphAPI, clientCred);

表示使用其应用标识(而不是用户的标识)调用API来获取访问令牌。您需要在azure广告门户中添加应用程序权限并向用户授予权限。选择应用程序权限意味着您的应用程序可以使用OAuth客户端凭据流来调用Graph API(无需用户)。请点击here了解详情。