我使用 Azure Active Directory (AAD) 设置了一个简单的 MVC 应用程序。
我需要查询 AAD Graph API 才能从我的应用程序管理应用程序角色和组。
In the Startup
类中,我收到了这样的 AccessToken:
public void ConfigureAuth(IAppBuilder app)
{
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = Constants.ClientId,
Authority = Constants.Authority,
PostLogoutRedirectUri = Constants.PostLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
// If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
AuthorizationCodeReceived = (context) =>
{
var code = context.Code;
var credential = new ClientCredential(Constants.ClientId, Constants.ClientSecret);
var signedInUserId =
context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
var authContext = new AuthenticationContext(Constants.Authority,
new TokenDbCache(signedInUserId));
var result = authContext.AcquireTokenByAuthorizationCode(
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential,
Constants.GraphUrl);
var accessToken = result.AccessToken;
return Task.FromResult(0);
}
}
});
}
实例化ActiveDirectoryClient
类,我需要传递 AccessToken :
var servicePointUri = new Uri("https://graph.windows.net");
var serviceRoot = new Uri(servicePointUri, tenantID);
var activeDirectoryClient = new ActiveDirectoryClient(serviceRoot,
async () => await GetTokenForApplication());
我想知道将 AccessToken 存储为声明是否是一个好的解决方案(在Startup
class)?
context.AuthenticationTicket.Identity.AddClaim(new
Claim("OpenId_AccessToken", result.AccessToken));
编辑令牌已存储..
得到它 !!!谢谢乔治。
所以我的令牌已使用以下方式存储在数据库中TokenDbCache
class.
根据示例再次获取它(在我的控制器之一中):
public async Task<string> GetTokenForApplication()
{
string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
// get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc)
ClientCredential clientcred = new ClientCredential(clientId, appKey);
// initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's database
AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID, new TokenDbCache<ApplicationDbContext>(signedInUserID));
AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenSilentAsync(graphResourceID, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
return authenticationResult.AccessToken;
}
我不知道的是AuthenticationContext
:
如果已经请求了令牌,它将从TokenDbCache
.