我正在开发一个旧的代码库,它使用ADAL静默令牌获取,我需要将它更新到MSAL。
代码如下所示:
public static async Task<string> AcquireToken(string userObjectId)
{
ClientCredential cred = new ClientCredential(ConfigHelper.ClientId, ConfigHelper.ClientSecret);
string tenantId = ClaimsPrincipal.Current.FindFirst(Globals.TenantIdClaimType).Value;
AuthenticationContext authContext = new AuthenticationContext(String.Format(CultureInfo.InvariantCulture, ConfigHelper.AadInstance, tenantId), new TokenDbCache(userObjectId));
AuthenticationResult result = await authContext.AcquireTokenSilentAsync(ConfigHelper.GraphResourceId, cred, new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));
return result.AccessToken;
}
public static async Task<string> AcquireToken(string resource, string userObjectId)
{
ClientCredential cred = new ClientCredential(ConfigHelper.ClientId, ConfigHelper.ClientSecret);
string tenantId = ClaimsPrincipal.Current.FindFirst(Globals.TenantIdClaimType).Value;
AuthenticationContext authContext = new AuthenticationContext(String.Format(CultureInfo.InvariantCulture, ConfigHelper.AadInstance, tenantId), new TokenDbCache(userObjectId));
AuthenticationResult result = await authContext.AcquireTokenSilentAsync(resource, cred, new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));
return result.AccessToken;
}我正试着阅读文件,但为了我的一生,我想不出该怎么做。
有什么帮助吗?
干杯
发布于 2022-09-19 08:50:59
这是我的实现,用于交互式地检索访问令牌(第一次)并在没有用户交互的情况下默默地更新令牌:
private static AuthenticationResult InitializeToken(EWSConnectionParameters param)
{
if (param.OAuthCredentials==null)
throw new UnauthorizedAccessException("OAUTH credentials not present");
var clientID = param.OAuthCredentials.ClientId;
var tenantId = param.OAuthCredentials.TenantId;
var instance = param.OAuthCredentials.Instance;
string[] scopes = param.OAuthCredentials.Scopes;
AuthenticationResult authResult = null;
IPublicClientApplication publicApp = PublicClientApplicationBuilder.Create(clientID)
.WithAuthority($"{instance}{tenantId}")
.WithDefaultRedirectUri()
.Build();
TokenCacheHelper.GetInstance().EnableSerialization(publicApp.UserTokenCache);
var accounts = publicApp.GetAccountsAsync().Result;
var firstAccount = accounts.FirstOrDefault();
try
{
//first try to silently get the token
authResult = publicApp.AcquireTokenSilent(scopes, firstAccount)
.ExecuteAsync().Result;
TraceWriter.Write(typeof(EwsProxyFactory), "InitializeToken", "The authentication token was acquired silently. Expiration time: " + authResult.ExpiresOn.DateTime.ToString());
}
catch (MsalUiRequiredException ex)
{
// A MsalUiRequiredException happened on AcquireTokenSilent, meaning that the token couldn't be acquired silently
TraceWriter.Write(typeof(EwsProxyFactory), "InitializeToken", "Failed to acquire the authentication token silently. The user needs to authenticate itself.");
//todo: create custom exception
throw new UnauthorizedAccessException("Token expired but running in silent mode");
}
catch (Exception ex)
{
TraceWriter.Write(typeof(EwsProxyFactory), "InitializeToken", "Error Acquiring Token Silently: " + ex);
throw;
}
TraceWriter.Write(typeof(EwsProxyFactory), "InitializeToken", "Token acquired");
return authResult;
}这是用于交互式登录的:
authResult = await publicApp.AcquireTokenInteractive(scopes)
.WithAccount(accounts.FirstOrDefault())
.WithPrompt(Prompt.SelectAccount)
.ExecuteAsync();https://stackoverflow.com/questions/73724524
复制相似问题