Setting ui-locales within Azure AD B2C sign in from within the application (.NET Core)

邮差的信 提交于 2021-01-28 05:54:59

问题


I am developing ASP.NET Core application which utilizes Azure AD B2C for user management. I would like to have sign in /up form being localized to multiple languages. I have added new languages for policies in Azure AD B2C management dashboard in Azure portal.

Now I would like to set ui-locales parameter (as described on this documentation page) when calling out to Azure AD B2C from my application. This is a piece which I am not able to achieve as I am not sure where I can inject this parameter and need help.


回答1:


There's two ways to support localization:

  • Let B2C serve the appropriate language based on the user's browser settings. For this, you shouldn't need to pass a ui_locales parameter, just enable localization, define the languages you want to support and B2C will do the right thing for you.

  • Alternatively, you can explicitly tell B2C which locale to serve up by sending the ui_locales query string parameter in your authorization request, for example: ui_locales=en-us

Full examples:

  • French (add ui_locales=fr-fr): https://login.microsoftonline.com/te/tdlrv2.onmicrosoft.com/b2c_1_susi/oauth2/v2.0/authorize?client_id=1fc82d11-53ed-45e9-ba60-82797f1c0f82&redirect_uri=https%3a%2f%2ftdlrb2c.azurewebsites.net%2f&response_mode=form_post&response_type=id_token&scope=openid&ui_locales=fr-fr

  • Spanish (add ui_locales=es-es): https://login.microsoftonline.com/te/tdlrv2.onmicrosoft.com/b2c_1_susi/oauth2/v2.0/authorize?client_id=1fc82d11-53ed-45e9-ba60-82797f1c0f82&redirect_uri=https%3a%2f%2ftdlrb2c.azurewebsites.net%2f&response_mode=form_post&response_type=id_token&scope=openid&ui_locales=es-es

In C# and ASP.Net, one option to do this is by adding the query string parameter in the OnRedirectToIdentityProvider event (see this example, though the event is used for something else, you'd use the same pattern).




回答2:


The Wingtip Games sample at here sets the ui_locales query string parameter in the OnRedirectToIdentityProvider event as follows:

// If an Account controller action has set a UI locale (e.g. to "fr"), then set the UI locales parameter for
// the authentication request to this UI locale.
if (context.Properties.Items.ContainsKey(Constants.AuthenticationPropertiesKeys.UILocales))
{
    context.ProtocolMessage.UiLocales = context.Properties.Items[Constants.AuthenticationPropertiesKeys.UILocales];
    context.Properties.Items.Remove(Constants.AuthenticationPropertiesKeys.UILocales);
}



回答3:


With some help of examples of @Chris Padgett I created this solution:

Create an AzureADB2COpenIdConnectOptionsConfigurator object:

using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace Forestbrook.Azure.ADB2C
{
    public class AzureADB2COpenIdConnectOptionsConfigurator : IConfigureNamedOptions<OpenIdConnectOptions>
    {
        public void Configure(string name, OpenIdConnectOptions options)
        {
            options.Events.OnRedirectToIdentityProvider = WrapOpenIdConnectEvent(options.Events.OnRedirectToIdentityProvider, OnRedirectToIdentityProvider);
        }

        public void Configure(OpenIdConnectOptions options)
            => Configure(Options.DefaultName, options);

        private static Task OnRedirectToIdentityProvider(RedirectContext context)
        {
            // TODO: Get your language setting
            var language = GetLanguage(context.HttpContext);

            if (!string.IsNullOrEmpty(language))
                context.ProtocolMessage.UiLocales = language;

            return Task.CompletedTask;
        }

        private static Func<TContext, Task> WrapOpenIdConnectEvent<TContext>(Func<TContext, Task> baseEventHandler, Func<TContext, Task> thisEventHandler)
            => async context => { await baseEventHandler(context); await thisEventHandler(context); };

        // Example: get selected language from standard Asp.Net Core cookie:
        private static string GetLanguage(HttpContext httpContext)
        {
            if (!httpContext.Request.Cookies.TryGetValue(CookieRequestCultureProvider.DefaultCookieName, out var value))
                return null;

            // Cookie value looks like: "c=nl-NL|uic=en-US". uic defines the language.
            var split = value.Split('=', '|');
            if (split.Length < 4)
                return null;

            return split[3];
        }
    }
}

In Startup.cs, in the ConfigureServices method, add this line AFTER AddAzureADB2C:

services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)
    .AddAzureADB2C(options => Configuration.Bind("AzureAdB2C", options));
// Add this line:
services.AddSingleton<IConfigureOptions<OpenIdConnectOptions>, AzureADB2COpenIdConnectOptionsConfigurator>();


来源:https://stackoverflow.com/questions/46333513/setting-ui-locales-within-azure-ad-b2c-sign-in-from-within-the-application-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!