问题描述
我正在使用.NET Core,并且试图使Web应用程序与Web API进行通信。两者都需要在所有类上使用 [Authorize] 属性进行身份验证。为了能够在服务器之间进行对话,我需要检索验证令牌。感谢。
I am using .NET Core, and I'm trying to make a web application talk to a web API. Both require authentication using the [Authorize] attribute on all of their classes. In order to be able to talk between them server-to-server, I need to retrieve the validation token. I've been able to do that thanks to a Microsoft tutorial.
在教程中,他们使用调用 AcquireTokenByAuthorizationCodeAsync 以便将令牌保存在缓存中,以便在其他地方,代码只需执行 AcquireTokenSilentAsync ,这不需要去授权来验证用户。
In the tutorial, they use a call to AcquireTokenByAuthorizationCodeAsync in order to save the token in the cache, so that in other places, the code can just do a AcquireTokenSilentAsync, which doesn't require going to the Authority to validate the user.
当用户已经登录时就会出现此问题。方法存储在 OpenIdConnectEvents.OnAuthorizationCodeReceived 永远不会被调用,因为没有收到授权。
The issue comes in when the user is already logged in. The method stored at OpenIdConnectEvents.OnAuthorizationCodeReceived never gets called, since there is no authorization being received. That method only gets called when there's a fresh login.
还有另一个事件叫做: CookieAuthenticationEvents.OnValidatePrincipal 。仅通过Cookie验证用户。这行得通,而且我可以获取令牌,但是我必须使用 AcquireTokenAsync ,因为那时我没有授权码。根据文档,它
There is another event called: CookieAuthenticationEvents.OnValidatePrincipal when the user is only being validated via a cookie. This works, and I can get the token, but I have to use AcquireTokenAsync, since I don't have the authorization code at that point. According to the documentation, it
这使调用 AcquireTokenSilentAsync 失败,因为令牌尚未缓存。而且我宁可不要总是使用 AcquireTokenAsync ,因为那总是交给管理局。
This makes calling AcquireTokenSilentAsync fail, since the token has not been cached. And I'd rather not always use AcquireTokenAsync, since that always goes to the Authority.
我如何告诉 AcquireTokenAsync 获得的令牌要被缓存,这样我才能使用 AcquireTokenSilentAsync 其他地方吗?
How can I tell the token gotten by AcquireTokenAsync to be cached so that I can use AcquireTokenSilentAsync everywhere else?
所有这些都来自Startup.cs文件在主要的Web应用程序项目中。
This all comes from the Startup.cs file in the main, Web Application project.
这是完成事件处理的方式:
This is how the event handling is done:
app.UseCookieAuthentication(new CookieAuthenticationOptions() { Events = new CookieAuthenticationEvents() { OnValidatePrincipal = OnValidatePrincipal, } }); app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions { ClientId = ClientId, Authority = Authority, PostLogoutRedirectUri = Configuration["AzureAd:PostLogoutRedirectUri"], ResponseType = OpenIdConnectResponseType.CodeIdToken, CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"], GetClaimsFromUserInfoEndpoint = false, Events = new OpenIdConnectEvents() { OnRemoteFailure = OnAuthenticationFailed, OnAuthorizationCodeReceived = OnAuthorizationCodeReceived, } });
这些是背后的事件:
private async Task OnValidatePrincipal(CookieValidatePrincipalContext context) { string userObjectId = (context.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value; ClientCredential clientCred = new ClientCredential(ClientId, ClientSecret); AuthenticationContext authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectId, context.HttpContext.Session)); AuthenticationResult authResult = await authContext.AcquireTokenAsync(ClientResourceId, clientCred); // How to store token in authResult? } private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext context) { // Acquire a Token for the Graph API and cache it using ADAL. In the TodoListController, we'll use the cache to acquire a token to the Todo List API string userObjectId = (context.Ticket.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value; ClientCredential clientCred = new ClientCredential(ClientId, ClientSecret); AuthenticationContext authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectId, context.HttpContext.Session)); AuthenticationResult authResult = await authContext.AcquireTokenByAuthorizationCodeAsync( context.ProtocolMessage.Code, new Uri(context.Properties.Items[OpenIdConnectDefaults.RedirectUriForCodePropertiesKey]), clientCred, GraphResourceId); // Notify the OIDC middleware that we already took care of code redemption. context.HandleCodeRedemption(); } // Handle sign-in errors differently than generic errors. private Task OnAuthenticationFailed(FailureContext context) { context.HandleResponse(); context.Response.Redirect("/Home/Error?message=" + context.Failure.Message); return Task.FromResult(0); }
可以在链接的教程中找到其他任何代码,或者询问,我将
Any other code can be found in the linked tutorial, or ask and I will add it to the question.
推荐答案
(注意:我一直在努力解决这一确切问题。遵循与问题链接相同的Microsoft教程,并跟踪了各种问题,例如追逐大雁;结果发现该示例在使用最新版本的 Microsoft时包含了许多看似不必要的步骤.AspNetCore.Authentication.OpenIdConnect 包。)。
当我阅读此页面时,我终于有了一个突破性的时刻:
I eventually had a breakthrough moment when I read this page: http://docs.identityserver.io/en/release/quickstarts/5_hybrid_and_api_access.html
该解决方案实质上涉及让OpenID Connect身份验证放置各种令牌( access_token ,刷新_token )放入Cookie。
The solution essentially involves letting OpenID Connect auth put the various tokens (access_token, refresh_token) into the cookie.
首先,我正在使用 融合应用 在和Azure AD终结点v2.0中创建。该应用程序具有应用程序秘密(密码/公钥),并在Web平台上使用允许隐式流。
Firstly, I'm using a Converged Application created at https://apps.dev.microsoft.com and v2.0 of the Azure AD endpoint. The App has an Application Secret (password/public key) and uses Allow Implicit Flow for a Web platform.
(由于某种原因,端点的v2.0似乎不适用于仅适用于Azure AD的应用程序。我不确定为什么,也不确定它是否真的很重要。)
(For some reason it seems as if v2.0 of the endpoint doesn't work with Azure AD only applications. I'm not sure why, and I'm not sure if it really matters anyway.)
Startup.Configure 方法中的相关行:
// Configure the OWIN pipeline to use cookie auth. app.UseCookieAuthentication(new CookieAuthenticationOptions()); // Configure the OWIN pipeline to use OpenID Connect auth. var openIdConnectOptions = new OpenIdConnectOptions { ClientId = "{Your-ClientId}", ClientSecret = "{Your-ClientSecret}", Authority = "http://login.microsoftonline.com/{Your-TenantId}/v2.0", ResponseType = OpenIdConnectResponseType.CodeIdToken, TokenValidationParameters = new TokenValidationParameters { NameClaimType = "name", }, GetClaimsFromUserInfoEndpoint = true, SaveTokens = true, }; openIdConnectOptions.Scope.Add("offline_access"); app.UseOpenIdConnectAuthentication(openIdConnectOptions);
就是这样!没有 OpenIdConnectOptions.Event 回调。没有调用 AcquireTokenAsync 或 AcquireTokenSilentAsync 。没有 TokenCache 。这些东西似乎都没有必要。
And that's it! No OpenIdConnectOptions.Event callbacks. No calls to AcquireTokenAsync or AcquireTokenSilentAsync. No TokenCache. None of those things seem to be necessary.
魔术似乎是 OpenIdConnectOptions.SaveTokens = true
这里是一个示例,其中我使用访问令牌代表使用其Office365帐户的用户发送电子邮件。
Here's an example where I'm using the access token to send an e-mail on behalf of the user using their Office365 account.
我有一个WebAPI控制器操作,该操作使用 HttpContext.Authentication.GetTokenAsync( access_token)获取访问令牌:
I have a WebAPI controller action which obtains their access token using HttpContext.Authentication.GetTokenAsync("access_token"):
[HttpGet] public async Task<IActionResult> Get() { var graphClient = new GraphServiceClient(new DelegateAuthenticationProvider(async requestMessage => { var accessToken = await HttpContext.Authentication.GetTokenAsync("access_token"); requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken); })); var message = new Message { Subject = "Hello", Body = new ItemBody { Content = "World", ContentType = BodyType.Text, }, ToRecipients = new[] { new Recipient { EmailAddress = new EmailAddress { Address = "[email protected]", Name = "Somebody", } } }, }; var request = graphClient.Me.SendMail(message, true); await request.Request().PostAsync(); return Ok(); }
注释#1
在某些时候,如果access_token过期,您可能还需要掌握 refresh_token :
HttpContext.Authentication.GetTokenAsync("refresh_token")
侧面注解2
我的 OpenIdConnectOptions 实际上包含一些我在这里省略的东西,例如:
Side Note #2
My OpenIdConnectOptions actually includes a few more things which I've omitted here, for example:
openIdConnectOptions.Scope.Add("email"); openIdConnectOptions.Scope.Add("Mail.Send");
我已经将它们用于与 Microsoft.Graph API,用于代表当前登录的用户发送电子邮件。
I've used these for working with the Microsoft.Graph API to send an e-mail on behalf of the currently logged in user.
(在应用程序上也设置了Microsoft Graph的那些委托权限)。
(Those delegated permissions for Microsoft Graph are set up on the app too).
到目前为止,此答案说明了如何使用缓存的访问令牌,但没有说明令牌过期时(通常在1小时后)如何使用。
So far, this answer explains how to use the cached access token but not what to do when the token expires (typically after 1 hour).
似乎是:
- 强制用户再次登录。 (不是沉默)
- 使用 refresh_token 将请求发布到Azure AD服务以获得新的 access_token (静默)。
- Force the user to sign in again. (Not silent)
- POST a request to the Azure AD service using the refresh_token to obtain a new access_token (silent).
如何使用端点v2.0刷新访问令牌
经过更多挖掘,我在此问题中找到了部分答案:
How to Refresh the Access Token using v2.0 of the Endpoint
After more digging, I found part of the answer in this SO Question:
似乎Microsoft OpenIdConnect库不会为您刷新访问令牌。不幸的是,以上问题的答案缺少有关如何刷新令牌的关键细节;大概是因为它取决于OpenIdConnect不在乎的有关Azure AD的特定详细信息。
It seems like the Microsoft OpenIdConnect libraries do not refresh the access token for you. Unfortunately the answer in the question above is missing the crucial detail about precisely how to refresh the token; presumably because it depends on specific details about Azure AD which OpenIdConnect doesn't care about.
上面问题的公认答案表明直接向Azure AD令牌发送请求REST API,而不使用其中一个Azure AD库。
The accepted answer to the above question suggests sending a request directly to the Azure AD Token REST API instead of using one of the Azure AD libraries.
这里是相关文档(注意:这涵盖了v1.0和v2.0的组合)
Here's the relevant documentation (Note: this covers a mix of v1.0 and v2.0)
- https://developer.microsoft.com/en-us/graph/docs/concepts/rest
- https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-code#refreshing-the-access-tokens
以下是基于API文档的代理:
Here's a proxy based on the API docs:
public class AzureAdRefreshTokenProxy { private const string HostUrl = "https://login.microsoftonline.com/"; private const string TokenUrl = $"{Your-Tenant-Id}/oauth2/v2.0/token"; private const string ContentType = "application/x-www-form-urlencoded"; // "HttpClient is intended to be instantiated once and re-used throughout the life of an application." // - MSDN Docs: // https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.110).aspx private static readonly HttpClient Http = new HttpClient {BaseAddress = new Uri(HostUrl)}; public async Task<AzureAdTokenResponse> RefreshAccessTokenAsync(string refreshToken) { var body = $"client_id={Your-Client-Id}" + $"&refresh_token={refreshToken}" + "&grant_type=refresh_token" + $"&client_secret={Your-Client-Secret}"; var content = new StringContent(body, Encoding.UTF8, ContentType); using (var response = await Http.PostAsync(TokenUrl, content)) { var responseContent = await response.Content.ReadAsStringAsync(); return response.IsSuccessStatusCode ? JsonConvert.DeserializeObject<AzureAdTokenResponse>(responseContent) : throw new AzureAdTokenApiException( JsonConvert.DeserializeObject<AzureAdErrorResponse>(responseContent)); } } }
JsonConvert 使用的> AzureAdTokenResponse 和 AzureAdErrorResponse 类:
The AzureAdTokenResponse and AzureAdErrorResponse classes used by JsonConvert:
[JsonObject(MemberSerialization = MemberSerialization.OptIn)] public class AzureAdTokenResponse { [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "token_type", Required = Required.Default)] public string TokenType { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "expires_in", Required = Required.Default)] public int ExpiresIn { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "expires_on", Required = Required.Default)] public string ExpiresOn { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "resource", Required = Required.Default)] public string Resource { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "access_token", Required = Required.Default)] public string AccessToken { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "refresh_token", Required = Required.Default)] public string RefreshToken { get; set; } } [JsonObject(MemberSerialization = MemberSerialization.OptIn)] public class AzureAdErrorResponse { [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "error", Required = Required.Default)] public string Error { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "error_description", Required = Required.Default)] public string ErrorDescription { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "error_codes", Required = Required.Default)] public int[] ErrorCodes { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "timestamp", Required = Required.Default)] public string Timestamp { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "trace_id", Required = Required.Default)] public string TraceId { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "correlation_id", Required = Required.Default)] public string CorrelationId { get; set; } } public class AzureAdTokenApiException : Exception { public AzureAdErrorResponse Error { get; } public AzureAdTokenApiException(AzureAdErrorResponse error) : base($"{error.Error} {error.ErrorDescription}") { Error = error; } }
最后,我对 Startup.cs的修改刷新 access_token
(基于我上面链接的答案)
Finally, my modifications to Startup.cs to refresh the access_token(Based on the answer I linked above)
// Configure the OWIN pipeline to use cookie auth. app.UseCookieAuthentication(new CookieAuthenticationOptions { Events = new CookieAuthenticationEvents { OnValidatePrincipal = OnValidatePrincipal }, });
启动中的 OnValidatePrincipal 处理程序.cs (同样,来自上面的链接答案):
The OnValidatePrincipal handler in Startup.cs (Again, from the linked answer above):
private async Task OnValidatePrincipal(CookieValidatePrincipalContext context) { if (context.Properties.Items.ContainsKey(".Token.expires_at")) { if (!DateTime.TryParse(context.Properties.Items[".Token.expires_at"], out var expiresAt)) { expiresAt = DateTime.Now; } if (expiresAt < DateTime.Now.AddMinutes(-5)) { var refreshToken = context.Properties.Items[".Token.refresh_token"]; var refreshTokenService = new AzureAdRefreshTokenService(); var response = await refreshTokenService.RefreshAccessTokenAsync(refreshToken); context.Properties.Items[".Token.access_token"] = response.AccessToken; context.Properties.Items[".Token.refresh_token"] = response.RefreshToken; context.Properties.Items[".Token.expires_at"] = DateTime.Now.AddSeconds(response.ExpiresIn).ToString(CultureInfo.InvariantCulture); context.ShouldRenew = true; } } }
最后,是使用Azure AD API v2.0的OpenIdConnect解决方案。
Finally, a solution with OpenIdConnect using v2.0 of the Azure AD API.
有趣的是,似乎v2.0并不需要资源包含在API请求中;文档认为有必要,但是API本身只是回答说不支持资源。这可能是一件好事-大概意味着访问令牌适用于所有资源(它当然适用于Microsoft Graph API)
Interestingly, it seems that v2.0 does not ask for a resource to be included in the API request; the documentation suggests it's necessary, but the API itself simply replies that resource is not supported. This is probably a good thing - presumably it means that the access token works for all resources (it certainly works with the Microsoft Graph API)
这篇关于如何使用Active Directory存储在AcquireTokenAsync中收到的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!