代码之家  ›  专栏  ›  技术社区  ›  FIL

如何使用MSAL代表使用AAD v1的用户获取访问令牌?

  •  1
  • FIL  · 技术社区  · 7 年前

    我有一个网络应用程序(Angular 7)使用 MSAL Angular here :

    • user_impersonation 对于Web API(委派)
    • User.Read; Mail.Send 对于MS Graph(委派)

    现在我想用ADAL for.NET从webapi调用microsoftwraph来获取一些数据 on behalf of a user .

    跟随 this instructions 我应该配置 consentScopes 和 protectedResourceMap 但是,由于我使用AAD v1,因此不能在增量同意的情况下使用作用域。

    我 found 代表 Web API与MS Graph的通信流程。

    更新

    以下是从Web API发出访问令牌请求的代码:

    string accessToken = null;
    var userAssertion = new UserAssertion(
        <userAccessToken>, 
        "urn:ietf:params:oauth:grant-type:jwt-bearer", 
        userName);
    var authority = "https://login.microsoftonline.com/" + <tenant> + "/";
    var clientCredencial = new ClientCredential(<clientId>, <clientSecret>);
    var authContext = new AuthenticationContext(authority, null);
    
    try
    {
        var authResult = await authContext.AcquireTokenAsync(
            "https://graph.microsoft.com", 
            clientCredencial, 
            userAssertion);
        accessToken = authResult.AccessToken;
    }
    catch (AdalServiceException ex) { throw; }
    

    知识应用程序 在Web API清单中:

    "knownClientApplications": [
        "<WebAppAppId>"
    ],
    

    以下是Web应用程序(MsalModule)中设置的范围:

    consentScopes:
    [
        'https://webapi.example.com/user_impersonation'
    ],
    

    在这种情况下,Web应用程序需要在同意屏幕上具有以下权限:

    • 随时访问您的数据

    如果我试图获取MS Graph的访问令牌,则会出现错误:

    {“AADSTS65001:用户或管理员未同意使用ID为“webapicentid”且名为“WebApiAppName”的应用程序。为此用户和资源发送交互式授权请求。}

    1 回复  |  直到 7 年前
        1
  •  0
  •   Philippe Signoret    7 年前

    假设您已经将Web API的应用程序注册配置为需要Microsoft Graph的相应委派权限,则有三个选项可提供所需的同意。

    1. this answer 获取截图和更多详细信息。)

    2. 通过构造适当的URL提供管理员许可。 使用门户授予管理员许可的另一种方法是构造同意URL。在本例中,您将为webapi构造同意URL。(参见 这个答案 获取截图和更多详细信息。)

    3. 在运行时,在同意Web应用程序期间,利用 knownClientApplications . 如果您添加 AppId 属于 致 知识应用程序 列出 的应用程序注册清单,当要求用户同意Web应用程序时,还会提示他们同意Web API也需要的任何权限。如果您使用v2端点登录Web应用程序(听起来像是这样),这种方法只在客户端请求默认作用域(即预先注册的权限,例如。 https://my.api.example.com/.default ).