小编Mit*_*tch的帖子

如何向 IdentityServer4 中的声明添加角色?

我是 IdentityServer 的新手,我整天都在为这个问题苦苦挣扎。如此之多以至于我几乎要放弃这个。我知道这个问题已经被一遍又一遍地问到,我尝试了许多不同的解决方案,但似乎没有一个有效。希望你能帮助我把我推向正确的方向。

首先,我通过运行安装了 IdentityServer4 模板,dotnet new -i identityserver4.templates并通过运行.is4aspid 模板创建了一个新项目dotnet new is4aspid -o IdentityServer

之后,我创建了一个新的 IdentityServer 数据库并运行了迁移。到那时,我有了一个默认的 Identity 数据库结构。

在 Config.cs 中,我更改MVC client为以下内容:

new Client
{
    ClientId = "mvc",
    ClientName = "MVC Client",

    AllowedGrantTypes = GrantTypes.Implicit,
    ClientSecrets = { new Secret("47C2A9E1-6A76-3A19-F3C0-S37763QB36D9".Sha256()) },

    RedirectUris = { "https://localhost:44307/signin-oidc" },
    FrontChannelLogoutUri = "https://localhost:44307/signout-oidc",
    PostLogoutRedirectUris = { "https://localhost:44307/signout-callback-oidc" },

    AllowOfflineAccess = true,
    AllowedScopes = { "openid", "profile", "api1", JwtClaimTypes.Role }                
},
Run Code Online (Sandbox Code Playgroud)

并将GetApis方法更改为:

public static IEnumerable<ApiResource> GetApis() …
Run Code Online (Sandbox Code Playgroud)

c# .net-core identityserver4

10
推荐指数
2
解决办法
1万
查看次数

方法'UseRouting'的重载没有1个参数

我刚刚更新到ASP.NET Core 3 Preview 5,现在,当我打开解决方案并尝试构建它时,在Configure()的Startup.cs文件中,在以下位置抛出错误“方法'UseRouting'的重载为1个参数”以下代码:

    app.UseRouting(routes => {
        routes.MapControllerRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
        routes.MapRazorPages();
    });
Run Code Online (Sandbox Code Playgroud)

我确实阅读了有关Microsoft文档的一些文档,并尝试将上述代码替换为:

    app.UseEndpoints(endpoints => {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
        endpoints.MapRazorPages();
    });
Run Code Online (Sandbox Code Playgroud)

但是,在生成期间抛出System.InvalidOperationException并带有以下上下文:

'EndpointRoutingMiddleware与EndpointMiddleware设置的端点匹配,因此必须在EndpointMiddleware之前添加到请求执行管道中。请通过在应用程序启动代码中对“ Configure(...)”的调用内调用“ IApplicationBuilder.UseRouting”来添加EndpointRoutingMiddleware。

我尝试在ConfigureServices方法中替换以下行:

    services.AddMvc()
        .AddNewtonsoftJson();
Run Code Online (Sandbox Code Playgroud)

宽度:

    services.AddControllersWithViews()
        .AddNewtonsoftJson();
    services.AddRazorPages();
Run Code Online (Sandbox Code Playgroud)

这不再引发错误,但加载完成后我的页面为空白。谁可以帮助我解决这个问题?

对于我的解决方案,我使用以下软件包:

 <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0-preview5.19227.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0-preview5.19227.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0-preview5.19227.9" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
Run Code Online (Sandbox Code Playgroud)

我的解决方案的TargetFramework是netcoreapp3.0

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

6
推荐指数
2
解决办法
2817
查看次数

如何使用 babel-jest 转译测试

我正在为 React 应用程序编写测试,并在编译我在测试文件中导入的类时遇到问题。运行测试套件时

\n\n
i get the following error:\nJest encountered an unexpected token\nThis usually means that you are trying to import a file which Jest cannot parse, e.g. it\'s not plain JavaScript.\nBy default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".\n\nHere\'s what you can do:\n \xe2\x80\xa2 To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.\n \xe2\x80\xa2 If you need a custom transformation specify a …
Run Code Online (Sandbox Code Playgroud)

reactjs babeljs babel-jest

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

Node.js - 一个承诺的模拟结果

我想模拟节点模块中函数的结果,以便我可以运行断言。考虑以下节点模块:

const doPostRequest = require('./doPostRequest.js').doPostRequest;
const normalizeSucessResult = require('./normalizer.js').normalizeSucessResult;
const normalizeErrorResult = require('./normalizer.js').normalizeErrorResult;

exports.doPost = (params, postData) => {
  return doPostRequest(params, postData).then((res) => {
    const normalizedSuccessResult = normalizeSucessResult(res);
    return normalizedSuccessResult;
  }).catch((err) => {
    const normalizedErrorResult = normalizeErrorResult(err);
    return normalizedErrorResult;
  })
}
Run Code Online (Sandbox Code Playgroud)

该函数doPostRequest返回一个承诺。我如何伪造这个承诺的返回值,以便我可以断言是否normalizeSucessResult已被调用?所以我已经尝试过:

const normalizeSucessResult = require('./normalizer.js');
const doPostRequest = require('./doPostRequests.js');
const doPost = require('./doPost.js');

it('runs a happy flow scenario', async () => {
  let normalizeSucessResultStub = sinon.stub(normalizeSucessResult, 'normalizeSucessResult');
  let postData = { body: 'Lorum ipsum' }; …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing mocking node.js sinon

1
推荐指数
2
解决办法
8375
查看次数