Identityserver4: Documentation Issue at docs/topics/cors.rst

Created on 24 Jul 2020  路  5Comments  路  Source: IdentityServer/IdentityServer4

This is not a bug per se, more of a documentation issue. My apologies if I should have categorized it differently.

I previously submitted this change as a pull request (https://github.com/IdentityServer/IdentityServer4/pull/4404/files). Again my apologies, I don't think that was the best way to communicate what I believe to be a documentation issue.

docs/topics/cors.rst indicates that you define Allowed Origins like this:
var cors = new DefaultCorsPolicyService(_loggerFactory.CreateLogger())
{
AllowedOrigins = { "https://foo", "https://bar" }
};
services.AddSingleton(cors);

However, this was not working for me. What I did eventually get working is:
services.AddSingleton((container) => {
var logger = container.GetRequiredService>();
return new DefaultCorsPolicyService(logger) {
AllowedOrigins = { "https://foo", "https://bar" }
};
});

I believe the reason that the former approach did not work is that an instance of _loggerFactory is no longer available in Startup.cs->ConfigureServices(). I believe this is due to a change Microsoft made in the approach to configuring logging in .NET Core 3.1.

bug report docs

All 5 comments

I am facing the same issue. the original code no longer works.

But the code suggested by @abarker11101 also does not work. it keeps saying that the ILogger provided by Microsoft is not compatible with the ILogger need by DefaultCorsPolicyService.

I think this code may work better:

services.AddSingleton<ICorsPolicyService>((container) => {
    var logger = container.GetRequiredService<ILogger<DefaultCorsPolicyService>>();
    return new DefaultCorsPolicyService(logger) {
        AllowedOrigins = { "https://foo", "https://bar" }
    };
});

@abarker11101 that worked.

Yes, you're absolutely right -- I made the change on my docs PR. Thank you!

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

agilenut picture agilenut  路  3Comments

osmankibar picture osmankibar  路  3Comments

eshorgan picture eshorgan  路  3Comments

chrisrestall picture chrisrestall  路  3Comments

wangkanai picture wangkanai  路  3Comments