代码之家  ›  专栏  ›  技术社区  ›  Carlo Mendoza

使用访问令牌和MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory进行LoginAsync

  •  0
  • Carlo Mendoza  · 技术社区  · 7 年前

    我得到一个“你没有权限查看这个目录或网页。”错误,当我尝试 LoginAsync MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory . 这与 MobileServiceAuthenticationProvider.MicrosoftAccount . 我不知道为什么这样不行。有没有我遗漏的配置?

    var msaProvider = await WebAuthenticationCoreManager.FindAccountProviderAsync(
    "https://login.microsoft.com",
    "https://login.microsoftonline.com/3dd13bb9-5d0d-dd2e-9d1e-7a966131bf85");
    string clientId = "6d15468d-9dbe-4270-8d06-a540dab3252f";
    WebTokenRequest request1 = new WebTokenRequest(msaProvider, "User.Read", clientId);
    request1.Properties.Add("resource", "https://graph.microsoft.com");
    WebTokenRequestResult result =
    await WebAuthenticationCoreManager.RequestTokenAsync(request1);
        if (result.ResponseStatus == WebTokenRequestStatus.Success)
        {
            var token = result.ResponseData[0].Token;
            var token1 = new JObject
                    {
                        { "access_token", token }
                    };
            var user = await App.mobileServiceClient.LoginAsync(
                MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory, token1);
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Carlo Mendoza    7 年前

    我能够让MSAL.NET为下面的代码工作。关键是 { resourceId + "/user_impersonation" } 范围。

    PublicClientApplication pca = new PublicClientApplication(clientId)
                        {
                            RedirectUri = redirectUri
                        };
                        string[] scopes = { resourceId + "/user_impersonation" };
                        var users = await pca.GetAccountsAsync();
                        var user = users.FirstOrDefault();
                        AuthenticationResult msalar = await pca.AcquireTokenAsync(
                            scopes, user, UIBehavior.ForceLogin, "domain_hint=test.net");
                             payload = new JObject
                             {
                                 ["access_token"] = msalar.AccessToken
                             };
     mobileServiceClient.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory, payload);
    

    https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/660#issuecomment-433831737

    推荐文章