如何解决正确的方法以个人用户身份访问blazor服务器端应用程序上的MS Graph数据
我在Blazor(ASP.NET Core)服务器端构建了一个Web应用程序。该应用程序是多租户,我需要控制谁有权访问,因此我使用了存储在SQL数据库中的自己的用户数据库(DefaultIdentity)。
现在,我需要访问Microsoft Graph(对于某些用户)。我通过使用应用程序级访问来完成这项工作,但希望避免让本地IT管理员在其Azure AD中进行设置-我想遵守“最低特权原则”。
因此,我希望能够添加一个登录页面/功能,以便特定用户(在他们使用本地用户数据库登录后)可以让我的应用程序获得批准并同意获取Calendar.Read权限,通过Microsoft图。
我应该怎么做?
我想我学到的是
- 我应该使用OAuth2.0授权代码流
- 我的应用程序是机密应用程序(我具有安全的服务器端)-但这阻止了我使用AquireTokenInteractive !?那我应该使用IPublicClientApplication吗?
我可以找到的所有示例都使用Azure AD登录用户。例如,可以手动查看授权代码流的示例吗?
解决方法
我现在有一个可行的解决方案,但想听听你们中的一些人是否有任何投入-我从未见过的任何警告或陷阱。
我的解决方法是:
我正在使用“标准流程”来获取PublicClientApplication
IPublicClientApplication app = PublicClientApplicationBuilder
.Create(publicAppClientId)
.WithRedirectUri("http://localhost")
.Build();
TokenCacheHelper.EnableSerialization(app.UserTokenCache);
var accounts = await app.GetAccountsAsync();
IAccount firstAccount = accounts.FirstOrDefault();
AuthenticationResult authResult = null;
try
{
authResult = await app.AcquireTokenSilent(scopesList,firstAccount)
.ExecuteAsync();
}
catch (MsalUiRequiredException)
{
try
{
authResult = await app.AcquireTokenInteractive(scopesList)
.ExecuteAsync();
}
catch (MsalException msalex)
{
System.Diagnostics.Debug.WriteLine($"Error acquiring Token:{System.Environment.NewLine}{msalex}");
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error acquiring token silently:{System.Environment.NewLine}{ex}");
return;
}
if (authResult != null)
{
System.Diagnostics.Debug.WriteLine($"Access token:{System.Environment.NewLine}{authResult.AccessToken}");
// Use the token
InteractiveAuthenticationProvider authProvider = new InteractiveAuthenticationProvider(app,scopesList);
GraphServiceClient = new GraphServiceClient(authProvider);
}
}
我已经使用TokenCacheHelper添加了分布式SQL缓存
public class TokenCacheHelper
{
private IDistributedCache _cache;
private string lookUpAccount = string.Empty;
private byte[] token = null;
public TokenCacheHelper(IDistributedCache cache,string user)
{
_cache = cache;
lookUpAccount = user;
}
public void EnableSerialization(ITokenCache tokenCache)
{
tokenCache.SetBeforeAccess(BeforeAccessNotification);
tokenCache.SetAfterAccess(AfterAccessNotification);
}
private void BeforeAccessNotification(TokenCacheNotificationArgs args)
{
if (token == null)
{
token = _cache.Get(lookUpAccount);
}
args.TokenCache.DeserializeMsalV3(token);
}
private void AfterAccessNotification(TokenCacheNotificationArgs args)
{
if (args.HasStateChanged)
{
token = args.TokenCache.SerializeMsalV3();
_cache.Set(lookUpAccount,token);
}
}
}
在startup.cs中添加DistributedSqlServerCache,令牌上的超时为“无限”
services.AddDistributedSqlServerCache(options =>
{
options.ConnectionString = Configuration.GetConnectionString("TestDbContext");
options.SchemaName = "dbo";
options.TableName = "TestCache";
options.DefaultSlidingExpiration = TimeSpan.FromDays(300);
options.ExpiredItemsDeletionInterval = TimeSpan.FromDays(300);
});
所有这些似乎都有效。有什么想法吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。