2016-05-23 2 views
3

Я использую Azure AD B2C для аутентификации в приложении AS2 AspNetCore RC2, это частично работает в том, что, когда я перехожу к действию, требующему аутентификации, я перенаправлен на страницу входа в B2C соответствующим образом. Когда я успешно вхожу в систему, я правильно перенаправляюсь на мою страницу приложения (и я могу увидеть поле id_token, соответствующее в параметрах запроса). К сожалению, промежуточное ПО проверки подлинности конвейера, похоже, не правильно обрабатывает параметры запроса перенаправления, так как оно сразу перенаправляет меня на страницу входа. Может ли кто-нибудь посоветовать?AspNetCore UseOpenIdConnectAuthentication

код я использую ниже:

public static void UseOAuth(this IApplicationBuilder app) 
{ 
    // By default, all middleware are passive/not automatic. Making cookie middleware automatic so that it acts on all the messages. 
    app.UseCookieAuthentication(new CookieAuthenticationOptions{ AutomaticAuthenticate = true, CookieSecure = CookieSecureOption.Never }); 

    app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions { 
     ClientId = B2CAuthentication.ClientId, 
     ResponseType = OpenIdConnectResponseTypes.IdToken, 
     Authority = string.Format(CultureInfo.InvariantCulture, B2CAuthentication.AadInstance, B2CAuthentication.PortalTenant, string.Empty, string.Empty), 
     AuthenticationScheme = "Cookies", 
     Events = new OpenIdConnectEvents 
     { 
      OnAuthenticationFailed = OnAuthenticationFailed, 
      OnRedirectToIdentityProvider = OnRedirectToIdentityProvider, 
      OnAuthorizationCodeReceived = OnAuthorizationCodeReceived, 
      OnTokenResponseReceived = OnTokenResponseReceived, 
      OnTokenValidated = OnTokenValidated, 
      OnTicketReceived = OnTicketReceived, 
      OnMessageReceived = OnMessageReceived, 
      OnRedirectToIdentityProviderForSignOut = OnRedirectToIdentityProviderForSignOut, 
      OnRemoteFailure = OnRemoteFailure, 
      OnUserInformationReceived = OnUserInformationReceived 
     }, 
     // The PolicyConfigurationManager takes care of getting the correct Azure AD authentication 
     // endpoints from the OpenID Connect metadata endpoint. It is included in the PolicyAuthHelpers folder. 
     ConfigurationManager = new PolicyConfigurationManager(
      string.Format(CultureInfo.InvariantCulture, B2CAuthentication.AadInstance, B2CAuthentication.PortalTenant, "/v2.0", "/" + OpenIdProviderMetadataNames.Discovery), 
      new string[] { B2CAuthentication.ResetPolicy, B2CAuthentication.CommonPolicy, B2CAuthentication.SignInPolicy }) 

    }); 
} 

private static Task OnUserInformationReceived(UserInformationReceivedContext arg) 
{ 
    ...Never called... 
} 

private static Task OnRemoteFailure(FailureContext arg) 
{ 
    ...Never called... 
} 

private static Task OnRedirectToIdentityProviderForSignOut(RedirectContext arg) 
{ 
    ...Never called... 
} 

private static Task OnMessageReceived(MessageReceivedContext arg) 
{ 
    ...Never called... 
} 

private static Task OnTicketReceived(TicketReceivedContext arg) 
{ 
    ...Never called... 
} 

private static Task OnTokenValidated(TokenValidatedContext arg) 
{ 
    ...Never called... 
} 

private static Task OnTokenResponseReceived(TokenResponseReceivedContext arg) 
{ 
    ...Never called... 
} 

private static Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext arg) 
{ 
    ...Never called... 
} 

private static async Task OnRedirectToIdentityProvider(RedirectContext context) 
{ 
    PolicyConfigurationManager mgr = (PolicyConfigurationManager)context.Options.ConfigurationManager; 
    if (context.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest) 
    { 
     OpenIdConnectConfiguration config = await mgr.GetConfigurationByPolicyAsync(CancellationToken.None, B2CAuthentication.CommonPolicy); 
     context.ProtocolMessage.IssuerAddress = config.EndSessionEndpoint; 
    } 
    else 
    { 
     OpenIdConnectConfiguration config = await mgr.GetConfigurationByPolicyAsync(CancellationToken.None, B2CAuthentication.CommonPolicy); 
     context.ProtocolMessage.IssuerAddress = config.AuthorizationEndpoint; 
     context.ProtocolMessage.RedirectUri = "http://localhost:8080/Portal/"; 
     context.ProtocolMessage.ResponseType = OpenIdConnectResponseTypes.IdToken; 
     context.ProtocolMessage.ResponseMode = OpenIdConnectResponseModes.Query; 
    } 
} 

private static Task OnAuthenticationFailed(AuthenticationFailedContext context) 
{ 
    context.HandleResponse(); 
    context.Response.Redirect("/Home/Error?message=" + context.Exception.Message); 
    return Task.FromResult(0); 
} 

ответ

4

мне удалось получить эту работу, выполнив следующие действия:

Фундаментально Я думаю, что использование CallbackPath, изменение на AuthenticationScheme и изменение ResponseMode на FormPost, все внесенные в исправление.

public static void UseOAuth(this IApplicationBuilder app) 
{ 
    // By default, all middleware are passive/not automatic. Making cookie middleware automatic so that it acts on all the messages. 
    app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     AutomaticAuthenticate = true, 
     CookieName = "MyCookieName", 
     CookieSecure = CookieSecureOption.Never, 
     AuthenticationScheme = "Cookies" 
    }); 

    JwtSecurityTokenHandler.DefaultInboundClaimTypeMap = new Dictionary<string, string>(); 

    app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions { 
     AutomaticAuthenticate = true, 
     Authority = string.Format(CultureInfo.InvariantCulture, B2CAuthentication.AadInstance, B2CAuthentication.PortalTenant, string.Empty, string.Empty), 
     ClientId = B2CAuthentication.ClientId, 
     ResponseType = OpenIdConnectResponseTypes.IdToken, 
     AuthenticationScheme = "oidc", 
     ResponseMode = OpenIdConnectResponseModes.FormPost, 
     CallbackPath = "/", 
     Scope = { "openid" }, 
     Events = new OpenIdConnectEvents 
     { 
      OnAuthenticationFailed = OnAuthenticationFailed, 
      OnRedirectToIdentityProvider = OnRedirectToIdentityProvider, 
      OnTokenValidated = OnTokenValidated, 
      OnRemoteFailure = OnRemoteFailure 
     }, 
     // The PolicyConfigurationManager takes care of getting the correct Azure AD authentication 
     // endpoints from the OpenID Connect metadata endpoint. It is included in the PolicyAuthHelpers folder. 
     ConfigurationManager = new PolicyConfigurationManager(
      string.Format(CultureInfo.InvariantCulture, B2CAuthentication.AadInstance, B2CAuthentication.PortalTenant, "/v2.0", "/" + OpenIdProviderMetadataNames.Discovery), 
      new string[] { B2CAuthentication.ResetPolicy, B2CAuthentication.CommonPolicy, B2CAuthentication.SignInPolicy }) 

    }); 
} 
+0

Быстрый вопрос, так как я не могу показаться, чтобы найти ответ, и вы, кажется, он работает. «OnTokenValidated» заменил «AuthenticationValidated» при переходе с RC1 на RC2? – Brandon

+0

btw, FormPost - значение по умолчанию, поэтому его не нужно устанавливать. его просто не нужно переопределять. – spottedmahn

4

Использование

ResponseType = OpenIdConnectResponseType.Code 

внутри

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions 
{ 
    ... 
}); 

, кажется, сделать запрос на промежуточное событие CodeReceived.

Также добавьте Microsoft.AspNetCore.Mvc.Formatters.Xml в project.json уйти от ошибки Could not load file or assembly System.Private.DataContractSerialization

+0

Я бы хотел вас выпить, сэр. Хотя OpenIdConnectResponseType.Code не сделал этого, OpenIdConnectResponseType.IdToken, похоже, сделал! спасибо –

Смежные вопросы