You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ASP.NET Core客户端配置传递ui_locales至IdentityServer4

如何在ASP.NET Core WebApp客户端传递ui_locales到IdentityServer4

我来帮你搞定这个问题——要把ui_locales参数传递给IdentityServer4,核心就是在客户端发起认证请求的环节,把这个参数附加到重定向到IdentityServer的请求里。下面分两种最常见的场景来讲解:


1. 使用ASP.NET Core OpenID Connect中间件(主流方案)

如果你的客户端是用ASP.NET Core自带的OpenID Connect中间件处理认证,那可以通过拦截重定向事件来注入ui_locales参数。

在你的Program.cs(.NET 6+)或者Startup.cs(.NET 5及更早版本)里,配置认证服务时做如下修改:

using System.Globalization;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;

var builder = WebApplication.CreateBuilder(args);

// 配置认证服务
builder.Services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
    // 替换成你的IdentityServer实际配置
    options.Authority = "https://your-identityserver-domain";
    options.ClientId = "your-client-id";
    options.ClientSecret = "your-client-secret";
    options.ResponseType = "code";
    options.Scope.Add("openid");
    options.Scope.Add("profile");

    // 关键:在重定向到IdentityServer前添加ui_locales参数
    options.Events = new OpenIdConnectEvents
    {
        OnRedirectToIdentityProvider = context =>
        {
            // 这里可以灵活获取要传递的语言值,举几个常用方案:
            // 方案1:用当前请求的文化(需要先配置RequestLocalization中间件)
            var currentLocale = CultureInfo.CurrentCulture.Name;
            
            // 方案2:从请求的查询参数获取(比如用户切换语言时传的?lang=zh-CN)
            // var currentLocale = context.HttpContext.Request.Query["lang"].FirstOrDefault() ?? "en-US";
            
            // 方案3:从用户Cookie/会话读取保存的偏好语言
            // var currentLocale = context.HttpContext.Request.Cookies["preferred_lang"] ?? "en-US";

            // 把ui_locales附加到认证请求中
            context.ProtocolMessage.UiLocales = currentLocale;
            // 要是UiLocales属性不生效,也可以直接塞到参数集合里:
            // context.ProtocolMessage.Parameters.Add("ui_locales", currentLocale);

            return Task.CompletedTask;
        }
    };
});

// 可选:配置RequestLocalization自动识别请求文化(配合方案1使用)
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
    var supportedCultures = new[]
    {
        new CultureInfo("en-US"),
        new CultureInfo("zh-CN"),
        new CultureInfo("fr-FR")
    };
    options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-US");
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
});

var app = builder.Build();

app.UseRequestLocalization(); // 启用本地化中间件
app.UseAuthentication();
app.UseAuthorization();

// ... 其他业务中间件配置

app.Run();

这个方法的核心是利用OnRedirectToIdentityProvider事件,在重定向到IdentityServer的最后一步,修改认证请求的参数,把ui_locales加进去。


2. 手动用IdentityModel发起认证请求

如果你的客户端是手动构建认证请求(比如用IdentityModel库),那直接在创建授权请求时指定UiLocales属性就行:

using IdentityModel.Client;

// 先获取IdentityServer的发现文档
var httpClient = new HttpClient();
var discoveryDocument = await httpClient.GetDiscoveryDocumentAsync("https://your-identityserver-domain");

// 构建授权请求
var authRequest = new AuthorizationRequest
{
    Address = discoveryDocument.AuthorizeEndpoint,
    ClientId = "your-client-id",
    RedirectUri = "https://your-client-redirect-url",
    ResponseType = "code",
    Scope = "openid profile",
    UiLocales = "zh-CN" // 直接设置ui_locales的值
};

// 生成授权URL后重定向过去
var authUrl = authRequest.CreateAuthorizationUrl();
return Redirect(authUrl);

额外注意点

  • 传递的ui_locales值要符合标准语言标签(比如zh-CN、en-US),IdentityServer4会根据这个值匹配对应的本地化资源。
  • 确保你在IdentityServer4的登录页面已经做好了本地化逻辑(比如根据ui_locales加载对应的.resx资源文件),这样参数传递过去后就能自动切换登录表单的语言了。

内容的提问来源于stack exchange,提问作者Stefano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:39:38