【问题标题】:Microsoft Graph-API and Azure Mobile AppsMicrosoft Graph-API 和 Azure 移动应用
【发布时间】:2018-03-09 21:46:41
【问题描述】:

我有一个移动应用程序并想使用 Microsoft Graph-API 进行身份验证。 我正在使用 Microsoft.Identity.Client 命名空间。我可以通过调用在那里获取令牌

authResult = await App.PublicClientApp.AcquireTokenAsync(_scopes);

当我通过调用将此令牌传递给我的移动应用时

azureUser = await App.MobileService.LoginWithMicrosoftAccountAsync(authResult.AccessToken);

我收到 MobileServiceInvalidOperationException“您无权查看此目录或页面”。

我已在应用程序注册门户中注册了我的应用程序。 Registration Portal

在 Azure 中它看起来像这样: Azure Portal

我错了什么???

亲切的问候,

马丁

【问题讨论】:

    标签: azure azure-mobile-services


    【解决方案1】:

    Easy Auth 不支持使用 Azure AD V2.0 颁发的访问令牌交换令牌(在本例中使用 MSAL 库)。

    要将客户端流程用于移动客户端,您可以使用 OneDrive 身份验证库从 Microsoft 帐户的端点获取令牌。 这是一个类似的线程供您参考:

    Desktop client flow for MicrosoftAccount access to Azure Mobile Service API

    下面的代码很适合我:

    public  async Task TestEasyAuthAsync()
    {
    
        string acess_token = await AcquireTokenForLiveAccount();
        string applicationUrl = "https://mobilefei.azurewebsites.net/";
        var mobileClient = new MobileServiceClient(applicationUrl);
    
        JObject token = new JObject();
        token.Add("access_token", acess_token);
        var user = await mobileClient.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount, token);
        Console.WriteLine($"UserID:\n{user.UserId}");
    
        var result = await mobileClient.InvokeApiAsync(@"/.auth/me");
        Console.Read();
    }
    
    public async  Task<string> AcquireTokenForLiveAccount()
    {
        var msAuth = new MsaAuthenticationProvider("57336bd5-a80f-4b48-a29a-07fdea6ef91e", "https://login.microsoftonline.com/common/oauth2/nativeclient", new string[] { "wl.signin", "wl.offline_access" });
        await msAuth.AuthenticateUserAsync();
        return msAuth.CurrentAccountSession.AccessToken;
    }
    

    【讨论】:

    • 嗨,你成就了我的一天!有用!非常感谢!还有一个问题:-(我需要确认此权限的访问:自动登录,始终信息访问,应用程序的基本配置文件,每次登录时如何存储?提前谢谢马丁
    • 这个问题似乎与应用注册的类型有关。要解决此问题,您可以注册 Live SDK 应用程序,而不是 融合应用程序。然后你可以使用public MsaAuthenticationProvider(string clientId, string clientSecret, string returnUrl, string[] scopes, CredentialCache credentialCache); 来新建一个MsaAuthenticationProvider 类并提供秘密。
    猜你喜欢
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2017-12-10
    相关资源
    最近更新 更多