Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何代表flow使用带有Azure AD的Cookie来获取对其他资源的访问令牌_Azure_Asp.net Core_Openid_Azure Active Directory - Fatal编程技术网

如何代表flow使用带有Azure AD的Cookie来获取对其他资源的访问令牌

如何代表flow使用带有Azure AD的Cookie来获取对其他资源的访问令牌,azure,asp.net-core,openid,azure-active-directory,Azure,Asp.net Core,Openid,Azure Active Directory,我有两个应用程序使用相同的azure active directory。应用程序A和应用程序B 应用程序A使用 app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions { AutomaticAuthenticate = true, AutomaticChallenge = true, ClientId = Configuration["Auth

我有两个应用程序使用相同的azure active directory。应用程序A和应用程序B

应用程序A使用

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
        {

            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            ClientId = Configuration["Authentication:AzureAd:ClientId"],
            Authority = Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"],
            ClientSecret = Configuration["Authentication:AzureAd:ClientSecret"],
            CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"],                      
            ResponseType = OpenIdConnectResponseType.CodeIdToken, 
            GetClaimsFromUserInfoEndpoint = true,
            SignInScheme = "Cookies",
            SaveTokens = true,                                                              
            Events = new OpenIdConnectEvents
            {
                OnAuthorizationCodeReceived = OnAuthorizationCodeReceived,
            }        

        });
我通过以下方式获取令牌,从而获得对应用程序B api服务资源的访问权:

private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext context)
    {         
        string userObjectId = (context.Ticket.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value;
        ClientCredential clientCred = new ClientCredential(Configuration["Authentication:AzureAd:ClientId"], Configuration["Authentication:AzureAd:ClientSecret"]);
        AuthenticationContext authContext = new AuthenticationContext(Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"]);
        AuthenticationResult authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(
            context.ProtocolMessage.Code, new Uri(context.Properties.Items[OpenIdConnectDefaults.RedirectUriForCodePropertiesKey]), clientCred, Configuration["Authentication:AzureAd:GraphResourceId"]);
我还使用cookies登录到应用程序A,其中包括:

app.UseCookieAuthentication(new CookieAuthenticationOptions()
        {
            AuthenticationScheme = "Cookies",
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            SlidingExpiration = true,
            ExpireTimeSpan = TimeSpan.FromHours(1),
            Events = new CookieAuthenticationEvents()
            {
                OnSignedIn = OnSignedIn,
                OnSigningIn = OnSigningIn,
                OnValidatePrincipal = OnValidatePrincipal                    
            }
        });
/* Account Controller SignIn() */
return Challenge(
            new AuthenticationProperties {
                AllowRefresh = true,
                IsPersistent = true,                                      
                RedirectUri = "/" }, OpenIdConnectDefaults.AuthenticationScheme);
现在,我的问题与我的访问令牌即将过期的其他问题类似,但我对应用程序a的登录cookie仍然有效,因此用户似乎登录良好,尽管缓存中没有令牌

我也问了一系列其他的问题,看看我的饼干事件

 Task OnValidatePrincipal(CookieValidatePrincipalContext arg) {

     var http = new HttpClient();
                var uri = "https://login.microsoftonline.com/<tenant>/oauth2/token";
                var client_id = "<my_client_id>";
                var scope = "https://graph.microsoft.com/mail.read";
                var refresh_token = "<saved_refresh_token_in_cookie_if_SaveTokens = true>";
                var redirect_uri = "https://localhost:20352/";
                var grant_type = "refresh_token";
                var client_secret = "<client_secret_from_azure>";
                var body = new List<KeyValuePair<string, string>>
                        {
                            new KeyValuePair<string, string>("client_id", client_id),
                            new KeyValuePair<string, string>("scope", scope),
                            new KeyValuePair<string, string>("refresh_token", refresh_token),
                            new KeyValuePair<string, string>("redirect_uri", redirect_uri),
                            new KeyValuePair<string, string>("grant_type", grant_type),
                            new KeyValuePair<string, string>("client_secret", client_secret)
                        };

                var content = new FormUrlEncodedContent(body);

                var result = http.PostAsync(uri, content).Result;
                var stringContent = result.Content.ReadAsStringAsync().Result;

                JObject jobject = JObject.Parse(stringContent);
                var token = jobject["access_token"].Value<string>();

是否有一种方法可以将新的资源访问令牌返回到用户应用程序B api调用的令牌库中,而不使用代表用户流的有效令牌/刷新令牌?

如果您丢失了访问令牌和刷新令牌,则必须将用户重定向到Azure AD以再次进行身份验证。他们可能仍然在那里进行身份验证,所以他们只是被重定向回您的应用程序以及授权代码

在我的一个项目中,我制作了一个异常过滤器,用于执行以下操作:

public void OnException(ExceptionContext filterContext)
{
    //If the error is a silent token acquisition exception from ADAL..
    if(filterContext.Exception is AdalSilentTokenAcquisitionException)
    {
        //Instead of the usual procedure, return a 401 which triggers the OpenIdConnect middleware redirection
        filterContext.Result = new HttpUnauthorizedResult();
        filterContext.ExceptionHandled = true;
    }
}
因此,如果在静默令牌获取失败时引发异常,只需吞下错误并将结果更改为401,这将触发OpenIdConnect中间件将用户发送到Azure AD


由于您有
AutomaticAuthenticate=true
,它应该这样做。

如果您丢失了访问令牌和刷新令牌,您必须将用户重定向到Azure AD以再次进行身份验证。他们可能仍然在那里进行身份验证,所以他们只是被重定向回您的应用程序以及授权代码

在我的一个项目中,我制作了一个异常过滤器,用于执行以下操作:

public void OnException(ExceptionContext filterContext)
{
    //If the error is a silent token acquisition exception from ADAL..
    if(filterContext.Exception is AdalSilentTokenAcquisitionException)
    {
        //Instead of the usual procedure, return a 401 which triggers the OpenIdConnect middleware redirection
        filterContext.Result = new HttpUnauthorizedResult();
        filterContext.ExceptionHandled = true;
    }
}
因此,如果在静默令牌获取失败时引发异常,只需吞下错误并将结果更改为401,这将触发OpenIdConnect中间件将用户发送到Azure AD


因为您有
AutomaticAuthenticate=true
,所以它应该这样做。

没有访问令牌或刷新令牌,没有。您必须将它们发送到Azure AD中进行身份验证。但缓存中不应该有刷新令牌吗?过去的14天(IIRC),所以没有必要进行重定向。我正在尝试处理应用程序A cookie进行身份验证的场景,但无论出于何种原因,资源B的访问/刷新令牌无效或被清除。因此,重定向到重新授权是从cookie中提取,并允许在没有访问令牌的情况下将其签名到应用程序a中。然后我需要清除cookie,并重定向以处理此问题?如果没有访问令牌或刷新令牌,则不需要。您必须将它们发送到Azure AD中进行身份验证。但您不应该在缓存中有刷新令牌吗?过去的14天(IIRC),所以没有必要进行重定向。我正在尝试处理应用程序A cookie进行身份验证的场景,但无论出于何种原因,资源B的访问/刷新令牌无效或被清除。因此,重定向到重新授权是从cookie中提取,并允许在没有访问令牌的情况下将其签名到应用程序a中。我需要清除cookie,然后重定向以处理此问题?HttpUnauthorizedResult应为ASP.NET Core MVC的UnauthorizedResult。HttpUnauthorizedResult应为ASP.NET Core MVC的UnauthorizedResult。