简体   繁体   English

具有附加MVC控制器的IdentityServer3自定义OWIN中间件

[英]IdentityServer3 Custom OWIN Middleware with additional MVC controllers

I've got an implementation of Identity Server 3, where dependent on the URL requested it's possible to have different settings provided to IDS (configured options etc). 我已经实现了Identity Server 3,其中取决于所请求的URL,可以为IDS提供不同的设置(配置选项等)。

This works fine as is, but I am having issues adding MVC controllers/pages to the app (as described https://github.com/IdentityServer/IdentityServer3/issues/1140 ) - I get a 404 exception when I try to load the new page. 这按原样工作正常,但是我在向应用程序中添加MVC控制器/页面时遇到了问题(如https://github.com/IdentityServer/IdentityServer3/issues/1140所述)-当我尝试加载MVC控制器/页面时,出现404异常新的一页。 If I replace coreApp.Use<IdentityServerWithTenantSwitchingMiddleware>(coreApp.Properties); 如果我替换coreApp.Use<IdentityServerWithTenantSwitchingMiddleware>(coreApp.Properties); with coreApp.UseIdentityServer(new IdentityServerWithTenantSwitchingMiddleware(null, null).GetOptions()); coreApp.UseIdentityServer(new IdentityServerWithTenantSwitchingMiddleware(null, null).GetOptions()); it works as expected, I'm certain the issues comes in my implementation of Invoke in IdentityServerWithTenantSwitchingMiddleware . 它按预期工作,我确定问题出在IdentityServerWithTenantSwitchingMiddleware实现Invoke时。

It seems to me as though I'm missing some bootstrap/setup somewhere that tells IDS that I want to use MVC controllers. 在我看来,好像我在某个地方缺少一些引导程序/安装程序,告诉我我想使用MVC控制器。 Or something. 或者其他的东西。 I've seen a couple of similar issues/questions around ( https://github.com/IdentityServer/IdentityServer3/issues/1934 ), but none relate to this exact issue. 我在( https://github.com/IdentityServer/IdentityServer3/issues/1934 )上看到了几个类似的问题/问题,但都与这个确切的问题无关。

I've reproduced this issue using the IdentityServer3 CustomUserService from GitHub - see my fork of CustomUserService demo (specifically IdentityServerWithTenantSwitchingMiddleware.cs and Startup.cs ). 我使用GitHub上的IdentityServer3 CustomUserService重现了此问题- 请参阅我的CustomUserService演示叉 (特别是IdentityServerWithTenantSwitchingMiddleware.csStartup.cs )。 You can see this issue if you login to the demo (password/username: alice) - it throws an exception trying to render the eula page. 如果您登录到演示(密码/用户名:alice),则可以看到此问题-尝试渲染eula页面时会引发异常。

I'll include the important bits of code below: 我将在下面包括一些重要的代码:

Startup.cs 启动文件

public void Configuration(IAppBuilder app)
{
    Log.Logger = new LoggerConfiguration()
                   .MinimumLevel.Debug()
                   .WriteTo.Trace()
                   .CreateLogger();

    app.Map("/core", coreApp =>
    {
         coreApp.Use<IdentityServerWithTenantSwitchingMiddleware>(coreApp.Properties);
    });
}

IdentityServerWithTenantSwitchingMiddleware IdentityServerWithTenantSwitchingMiddleware

public override Task Invoke(IOwinContext context)
{
    var app = new AppBuilder();

    //Do some magic based on current request

    var options = GetOptions();

    _properties.ForEach(kvp =>
    {
        if (!app.Properties.ContainsKey(kvp.Key))
        {
            app.Properties.Add(kvp.Key, kvp.Value);
        }
    });

    app.UseIdentityServer(options);
     return app.Build()(context.Environment);
}

private IdentityServerOptions GetOptions()
{
    var factory = new IdentityServerServiceFactory()
        .UseInMemoryClients(Clients.Get())
        .UseInMemoryScopes(Scopes.Get());

    // different examples of custom user services
    var userService = new EulaAtLoginUserService();

    // note: for the sample this registration is a singletone (not what you want in production probably)
    factory.UserService = new Registration<IUserService>(resolver => userService);

    var options = new IdentityServerOptions
    {
        SiteName = "IdentityServer3 - CustomUserService",

        SigningCertificate = Certificate.Get(),
        Factory = factory,

        AuthenticationOptions = new AuthenticationOptions
        {
            LoginPageLinks = new LoginPageLink[] {
                    new LoginPageLink{
                        Text = "Register",
                        Href = "localregistration"
                    }
                }
        },

        EventsOptions = new EventsOptions
        {
            RaiseSuccessEvents = true,
            RaiseErrorEvents = true,
            RaiseFailureEvents = true,
            RaiseInformationEvents = true
        }
    };

    return options;
}

Any/all help/suggestions appreciated. 任何/所有帮助/建议表示赞赏。

Alex 亚历克斯

Have found an answer to my issue. 找到了我问题的答案。

Taken from https://github.com/damianh/DynamicKatanaPipeline - it looks as though I was not running the next steps in the pipeline. 取自https://github.com/damianh/DynamicKatanaPipeline-好像我没有在管道中进行下一步。 Working code here, in case it helps anyone: 如果可以帮助任何人,请在这里工作代码:

public override Task Invoke(IOwinContext context)
{
    var app = ConfigurePipeline(context);
    var dynamicAppFunc = app.Build();
    return dynamicAppFunc(context.Environment);
}

private IAppBuilder ConfigurePipeline(IOwinContext context)
{
    var app = new AppBuilder();

    app.UseIdentityServer(GetOptions(context));

    app.Run(_next.Invoke);

    return app;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM