Keycloak client for ASP.NET Core

前端 未结 3 1216
抹茶落季
抹茶落季 2020-12-23 15:11

Is there any existing Keycloak client for Asp.net Core? I have found a NuGet package for .net but it doesn\'t work with Core. Do you have any ideas how to easily integrate w

3条回答
  •  一整个雨季
    2020-12-23 15:39

    The thing that worked for us was setting these things in Startup.cs (it's cookie based authentication):

    public void Configure(...)
    {
        (...)
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme,
            AutomaticAuthenticate = true,
            CookieHttpOnly = true,
            CookieSecure = CookieSecurePolicy.SameAsRequest
        });
    
        app.UseOpenIdConnectAuthentication(CreateOpenIdConnectOptions(_customConfig));
        (...)
    }
    

    And setting up the options:

    private OpenIdConnectOptions CreateOpenIdConnectOptions(CustomConfigurationFile configuration)
    {
        var options = new OpenIdConnectOptions
        {
            AuthenticationScheme = "oidc",
            SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme,
            Authority = configuration.ServerAddress + "/auth/realms/" + configuration.Realm,
            RequireHttpsMetadata = true,
            PostLogoutRedirectUri = configuration.SystemAddress,
            ClientId = configuration.ClientId,
            ClientSecret = configuration.ClientSecret,
            ResponseType = OpenIdConnectResponseType.Code,
            GetClaimsFromUserInfoEndpoint = true,
            SaveTokens = true
        };
        options.Scope.Clear();
        options.Scope.Add("openid");
        return options;
    }
    

提交回复
热议问题