【问题标题】:Retrieving a persistent token for Azure user access检索 Azure 用户访问的持久令牌
【发布时间】:2021-11-11 03:41:24
【问题描述】:

我正在处理一个需要访问用户邮箱的项目(类似于 MS Flow 邮箱连接器的工作方式),这对于用户在网站上时很好,因为我可以从图表中访问他们的邮箱和正确的权限请求。我遇到的问题是我需要一个网络作业来在他们获得许可后持续监控用户的邮件文件夹。我知道我可以使用应用程序请求而不是委托请求,但我怀疑我的公司会签署此协议。有没有办法在用户离开网站后持久持有一个天蓝色令牌来访问用户信息..例如在网络工作中?

编辑

也许我判断错了,用户在 Web 应用程序中针对请求范围内的 Azure 应用程序进行身份验证

let mailApp : PublicClientApplication = new PublicClientApplication(msalAppConfig);
      let mailUser = mailApp.getAllAccounts()[0];
      let accessTokenRequest = {
        scopes : [ "User.Read", "MailboxSettings.Read", "Mail.ReadWrite", "offline_access" ],
        account : mailUser,
      }
      mailApp.acquireTokenPopup(accessTokenRequest).then(accessTokenResponse => {
.....
}

这会返回经过身份验证的正确响应。

然后我想在我尝试使用的控制台应用程序/Web 作业中使用此用户身份验证

var app = ConfidentialClientApplicationBuilder.Create(ClientId)
                                          .WithClientSecret(Secret)
                                          .WithAuthority(Authority, true)
                                          .WithTenantId(Tenant)
                                          .Build();

                System.Threading.Tasks.Task.Run(async () =>
                {
                    IAccount test = await app.GetAccountAsync(AccountId);
                }).Wait();

但是 GetAccountAsync 总是返回为空?

【问题讨论】:

  • 这就是刷新令牌的用途 :) 如果您的应用请求 offline_access 范围,您将获得一个刷新令牌,您可以在应用中的任何位置使用它来获取新的访问令牌和新的刷新令牌。
  • 检查了“Offline_scope”,但似乎无法让控制台应用程序获取用户详细信息.. 有什么想法吗? (上面的代码)
  • 啊好吧。由于您有一个前端单页应用程序,因此获得有用刷新令牌的唯一方法是在后端。因此,您需要调用您的后端并使用代表方法在那里获取令牌。并配置该后端+您的后台作业以使用具有相同秘密存储的相同令牌缓存类。
  • 谢谢你,我会调查的。

标签: c# azure security authentication access-token


【解决方案1】:

@juunas 正确的是,令牌会根据需要刷新并使用 AcquireTokenOnBehalfOf 函数。如果可能的话,应该把答案归功于他吗?

使用我的代码,返回的 idToken 可以在其他任何地方使用来访问资源。由于我的后端 WebJob 是连续的,因此我可以使用存储的令牌来访问资源并在令牌过期之前定期刷新令牌。

Angular 应用程序:

let mailApp : PublicClientApplication = new PublicClientApplication(msalAppConfig);
let mailUser = mailApp.getAllAccounts()[0];
let accessTokenRequest = {
    scopes : [ "User.Read", "MailboxSettings.Read", "Mail.ReadWrite", "offline_access" ],
    account : mailUser,
}
mailApp.acquireTokenPopup(accessTokenRequest).then(accessTokenResponse => {
    let token : string = accessTokenResponse.idToken;
}

在后端,在 API、webJob 或控制台中:

var app = ConfidentialClientApplicationBuilder.Create(ClientId)
                                                      .WithClientSecret(Secret)
                                                      .WithAuthority(Authority, true)
                                                      .WithTenantId(Tenant)
                                                      .Build();
            
var authProvider = new DelegateAuthenticationProvider(async (request) => {
   // Use Microsoft.Identity.Client to retrieve token
   List<string> scopes = new List<string>() { "Mail.ReadWrite", "MailboxSettings.Read", "offline_access", "User.Read" };
   var assertion = new UserAssertion(YourPreviouslyStoredToken);
   var result = await app.AcquireTokenOnBehalfOf(scopes, assertion).ExecuteAsync();
            
   request.Headers.Authorization =
                                    new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", result.AccessToken);
});
var graphClient = new GraphServiceClient(authProvider);
var users = graphClient.Me.MailFolders.Request().GetAsync().GetAwaiter().GetResult();

【讨论】:

    猜你喜欢
    • 2021-10-24
    • 2013-01-29
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 2021-09-05
    • 2012-08-23
    相关资源
    最近更新 更多