小编Rau*_*ros的帖子

在 ASP.NET MVC 中声明全局 Razor Page 变量 [非静态]

我有一个注入“_ViewImports”的 ConfigurationService 类

@inject IConfigurationService ConfigurationService
Run Code Online (Sandbox Code Playgroud)

并在每个视图页面访问它,如下所示:

@{
    // Configurations
    var configuration = await ConfigurationService.GetGeneralConfiguration();
    var currencySymbol = configuration.Currency.Symbol;
}
Run Code Online (Sandbox Code Playgroud)

这有效,但我必须复制和粘贴每个页面的代码。我试过写代码,_Layout.cshtml但没有用。静态变量也不起作用,因为值不是静态的,每个请求我都会获取当前用户配置并注入页面。所有需要currencySymbol变量值的页面我都需要粘贴代码,我不想要这个。

我的问题是:我怎么能在声明变量,例如_Layout.cshtml,或_ViewStart.cshtml或其他,这就是可以被所有的Razor视图页面来访问,如继承。此变量必须包含 ConfigurationService 值。

对不起我的英语不好。

c# razor asp.net-core-mvc asp.net-core

4
推荐指数
1
解决办法
1933
查看次数

使用IdentityServer 4时如何在Api Project中添加其他声明

对不起我的英语不好。

我有三个项目:IdentityServer,Ensino.Mvc,Ensino.Api。IdentityServer项目提供了IdentityServer4库中的主要身份信息和声明-声明配置文件,声明地址,声明Sid ...等。Ensino.Mvc项目以令牌形式获取此信息,并将其发送到API,以便MVC被授予对资源的访问权限。令牌包含IdentityServer提供的所有声明。但是在API中,我需要生成其他特定于API的声明,例如:声明EnrollmentId,它对应于令牌中的声明Sid。我也想在HttpContext中添加此声明以供将来使用。有人可以告诉我如何实现吗?

我在Startup.ConfigureServices中有以下代码:

// Add identity services
        services.AddAuthentication("Bearer")
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = "http://localhost:5100";
                options.RequireHttpsMetadata = false;
                options.ApiName = "beehouse.scope.ensino-api";
            });

        // Add mvc services
        services.AddMvc();
Run Code Online (Sandbox Code Playgroud)

在没有API的其他Project中,只有mvc,我已经继承UserClaimsPrincipalFactory并重写CreateAsync以添加其他声明。我喜欢在API项目中做类似的事情。可能吗?

最好的方法是什么?

编辑:经过一些研究,我想做的是:通过IdentityServer进行身份验证,并根据声明和特定的api数据库数据在api中设置授权。

asp.net-mvc claims-based-identity asp.net-identity asp.net-core identityserver4

4
推荐指数
1
解决办法
1286
查看次数