ASP.NetIdentityBlazor:如何从_Host.cshtml访问ClientId和客户端的RedirectUris?
创始人
2024-09-19 00:00:54
0
  1. 首先,在 Startup.cs 中添加以下代码:
services.AddAuthentication(options =>
    {
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    })
    .AddCookie()
    .AddOpenIdConnect(options =>
    {
        options.Authority = Configuration.GetValue("AuthServer:Authority");
        options.ClientId = Configuration.GetValue("AuthServer:ClientId");
        options.ClientSecret = Configuration.GetValue("AuthServer:ClientSecret");
        options.ResponseType = "code";
        options.RequireHttpsMetadata = false;
        options.Scope.Add("openid");
        options.Scope.Add("profile");
    });
  1. 在 _Host.cshtml 中,您可以使用以下代码访问 ClientId 和 RedirectUris:
@inject Microsoft.AspNetCore.Components.Services.IUriHelper UriHelper

@{
    var currentUri = UriHelper.GetAbsoluteUri();
    var clientId = Configuration.GetValue("AuthServer:ClientId");
    var redirectUris = Configuration.GetSection("AuthServer:RedirectUris").Get();
}

在上面的代码中,我们使用了 UriHelper 来获取当前的 URL(绝对 URI)。我们还使用 Configuration 来获取我们需要的配置值。

  1. 然后,您可以在需要用到这些值的地方使用这些变量。

例如,您可以将 RedirectUris 传递给 OpenIdConnect 的 Options,如下所示:

.AddOpenIdConnect(options =>
{
    // other options
    options.RedirectUri = redirectUris[0];
})

请注意,我们可以使用 redirectUris[0] 来获取第一个 RedirectUri。如果您有多个 URI,您可以根据需要使用其中一个。

这样,您就可以从 _Host.cshtml 访问 ClientId 和 RedirectUris。

相关内容

热门资讯

三分钟积累!poker mas... 三分钟积累!poker master辅助(透视)一贯存在有辅助插件(哔哩哔哩)1、poker mas...
六分钟教程书!hhpoker外... 六分钟教程书!hhpoker外开挂靠谱吗(透视)真是真的是有辅助透视(哔哩哔哩)1、每一步都需要思考...
2分钟诀窍!wepoker可以... 2分钟诀窍!wepoker可以开透视吗(透视)一直存在有辅助插件(哔哩哔哩)1、wepoker可以开...
九分钟妙计!wepoker黑侠... 九分钟妙计!wepoker黑侠辅助器(透视)一直是有辅助透视(哔哩哔哩)1、下载好wepoker黑侠...
第6分钟要领!wepokerp... 第6分钟要领!wepokerplus作必弊(透视)一直存在有辅助教程(哔哩哔哩)所有人都在同一条线上...
一分钟秘籍!wepoker到底... 一分钟秘籍!wepoker到底有没有透视(透视)确实是真的有辅助开挂(哔哩哔哩)1、在wepoker...
2分钟教材!wepoker透视... 2分钟教材!wepoker透视有吗(透视)一贯有辅助辅助(哔哩哔哩)亲,关键说明,wepoker透视...
第2分钟资料!wepoker分... 第2分钟资料!wepoker分析(透视)好像真的是有辅助插件(哔哩哔哩)1、进入到wepoker分析...
第2分钟步骤!德普之星私人局透... 第2分钟步骤!德普之星私人局透视(透视)一贯是有辅助下载(哔哩哔哩)1、该软件可以轻松地帮助玩家将德...
第九分钟手册!哈糖大菠萝破解器... 第九分钟手册!哈糖大菠萝破解器(透视)确实真的是有辅助插件(哔哩哔哩)1、哈糖大菠萝破解器有没有辅助...