【问题标题】:Microsoft Graph API beta phone Authentication update fails from c# web api methodMicrosoft Graph API beta 手机身份验证更新从 c# web api 方法失败
【发布时间】:2020-10-19 08:52:17
【问题描述】:

使用 Microsoft 图形 API,我可以使用 PostMan 工具使用手机号码更新电话身份验证方法部分。

基于该方法,我创建了一个 Web API 方法,该方法必须使用用户的手机号码更新电话身份验证方法部分。但失败并出现错误

用户未经身份验证

这是为了让 MFA where-in 用户输入发送到给定手机号码的一次性密码。

我写的代码是:


>     using System;
>     using System.Collections.Generic;
>     using System.Net.Http.Headers;
>     using System.Threading.Tasks;
>     
>     using Microsoft.Extensions.Options;
>     using Microsoft.Graph;
>     using Microsoft.Graph.Auth;
>     using Microsoft.Identity.Client;
>     
>     using UseGraphAPI.Interfaces;
>     using UseGraphAPI.Models;
>     
>     namespace UseGraphAPI.Repository
>     {
>         public class UserManager : IUserManager
>         {
>             private readonly GraphServiceClient graphClient;
>             private readonly B2CUserSettings userSettings;
>             private readonly AuthenticationResult token;
>     
>             public UserManager(IOptions<B2CUserSettings> userSettings)
>             {
>                 // The client_id, client_secret, and tenant are pulled in from the appsettings.json from coach API
>                 this.userSettings = userSettings.Value;
>     
>                 // Initialize the client credential auth provider
>                 IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
>                     .Create(this.userSettings.Clientid)
>                     .WithTenantId(this.userSettings.Tenant)
>                     .WithClientSecret(this.userSettings.Clientsecret)
>                     //.WithRedirectUri("http://localhost:62569")
>                     //.WithAuthority("https://login.microsoftonline.com/29fdf6e9-53c9-4bc7-8927-fa50e62019bc/v2.0")
>                     .Build();
>     
>                 ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
>     
>                 // Set up the Microsoft Graph service client with client credentials
>                 GraphServiceClient graphClient = new GraphServiceClient(authProvider);
>     
>                 //string[] scopes = new string[] {
>                 //    "https://graph.microsoft.com/.default"
>                 //};
>     
>                 //token = confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync().Result;
>                 //GraphServiceClient graphClient = new GraphServiceClient("https://graph.microsoft.com/beta", 
>                 //    new DelegateAuthenticationProvider(async(requestMessage) => 
>                 //    {
>                 //    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token.AccessToken);
>                 //    }));
>     
>                 this.graphClient = graphClient;
>             }
>     
>             public async Task UpdateUserPhoneMethod(B2CUserPhoneAuth b2CUserPhoneAuth)
>             {
>                 var user = await GetUserByEmail(b2CUserPhoneAuth.Email);
>                 var userId = user.CurrentPage[0].Id;
>     
>                 try
>                 {
>                     PhoneAuthenticationMethod phoneAuthenticationMethod = new PhoneAuthenticationMethod()
>                     {
>                         PhoneNumber = b2CUserPhoneAuth.PhoneNumber,
>                         PhoneType = AuthenticationPhoneType.Mobile
>                     };
>     
>                     await graphClient.Users[userId].Authentication.PhoneMethods
>                         .Request()
>                         .AddAsync(phoneAuthenticationMethod);
>                 }
>                 catch (Exception ex)
>                 {
>                     throw ex;
>                 }
>             }
>         }
>     }

在 Azure 中注册的应用程序授予的权限是:

Directory.AccessAsUser.All(委托) Directory.ReadWrite.All (委托和应用)Policy.Read.All(委托) Policy.ReadWrite.AuthenticationMethod(委托) User.ReadWrite.All (委托和应用程序)UserAuthenticationMethod.ReadWrite.All (委托&申请)

如果我的代码或权限中缺少某些内容,请查看并告诉我。

要求是创建用户并添加手机短信登录标志为true。

问候,

阿朱那

【问题讨论】:

  • ClientCredentialProvider使用应用权限,但API不支持应用权限,见here。您只能使用文档显示的委托权限。
  • 在以下帖子中查看我的回复:stackoverflow.com/questions/64375532/…
  • @jdweng,我看到了您发布的 URL,发现它使用的是 HttpClient。但是,如果您看到我的代码,我使用的是没有选项的 MS graph API beta 版本。您能否建议在我的代码中是否有可以实现的方法。
  • @jdweng,在这行代码 await graphClient.Users[userId].Authentication.PhoneMethods .Request() .AddAsync(phoneAuthenticationMethod);它抛出以下错误代码:未经身份验证的消息:用户未经身份验证。内部错误:消息:用户未经身份验证。 AdditionalData:日期:2020-10-19T10:16:41 request-id:904355cc-df61-4428-89dc-b8dc08b27646 client-request-id:904355cc-df61-4428-89dc-b8dc08b27646 ClientRequestId:904355cc-df61-4428-89dc -b8dc08b27646

标签: c# azure microsoft-graph-api beta-versions


【解决方案1】:

正如我在评论中所说,代码ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication); 是基于具有应用程序权限的客户端凭据流。但API 只支持委托权限。

您可以使用其他方法(例如AuthorizationCodeProvider)代替它。

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                .Create(clientId)
                .WithRedirectUri(redirectUri)
                .WithClientSecret(clientSecret) // or .WithCertificate(certificate)
                .Build();

AuthorizationCodeProvider authenticationProvider = new AuthorizationCodeProvider(confidentialClientApplication, scopes);

【讨论】:

    猜你喜欢
    • 2017-09-20
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    相关资源
    最近更新 更多