ASP.NET Core的Keycloak客户端
发布时间:2020-12-15 23:06:44 所属栏目:asp.Net 来源:网络整理
导读:是否存在 Asp.net Core的Keycloak客户端? I have found a NuGet package for .net但它不适用于Core.您是否有任何想法如何轻松地与此安全服务器集成(或可能使用任何其他替代方案)? 解决方法 我今天玩了一下这个.最简单的方法是使用OpenId标准. 在Startup.cs
是否存在
Asp.net Core的Keycloak客户端?
I have found a NuGet package for .net但它不适用于Core.您是否有任何想法如何轻松地与此安全服务器集成(或可能使用任何其他替代方案)?
解决方法
我今天玩了一下这个.最简单的方法是使用OpenId标准.
在Startup.cs中,我使用了OpenIdConnect身份验证: public void Configure(...) { (...) app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme,AutomaticAuthenticate = true,CookieHttpOnly = true,CookieSecure = CookieSecurePolicy.SameAsRequest }); app.USEOpenIdConnectAuthentication(CreateKeycloakOpenIdConnectOptions());`(...) }` OpenIdConnectOptions方法: private OpenIdConnectOptions CreateKeycloakOpenIdConnectOptions() { var options = new OpenIdConnectOptions { AuthenticationScheme = "oidc",SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme,Authority = Configuration["Authentication:KeycloakAuthentication:ServerAddress"]+"/auth/realms/"+ Configuration["Authentication:KeycloakAuthentication:Realm"],RequireHttpsMetadata = false,//only in development PostLogoutRedirectUri = Configuration["Authentication:KeycloakAuthentication:PostLogoutRedirectUri"],ClientId = Configuration["Authentication:KeycloakAuthentication:ClientId"],ClientSecret = Configuration["Authentication:KeycloakAuthentication:ClientSecret"],ResponseType = OpenIdConnectResponseType.Code,GetClaimsFromUserInfoEndpoint = true,SaveTokens = true }; options.Scope.Add("openid"); return options; } 在appsettings.json中添加Keycloak的配置: { (...),"Authentication": { "KeycloakAuthentication": { "ServerAddress": "http://localhost:8180","Realm": "demo","PostLogoutRedirectUri": "http://localhost:57630/","ClientId": "KeycloakASPNETCore","ClientSecret": "secret-get-it-in-keycloakConsole-client-credentials" } } } Keycloak客户端配置如下: > Client settings, 如果我想按角色授权用户,我会这样做: 在ConfigureServices方法中添加authorization by claims: public void ConfigureServices(IServiceCollection services) { (...) services.AddAuthorization(options => { options.AddPolicy("Accounting",policy => policy.RequireClaim("member_of","[accounting]")); //this claim value is an array. Any suggestions how to extract just single role? This still works. }); } 我在ValuesController(默认Web API模板)中编辑了get方法: [Authorize(Policy = "Accounting")] [Route("api/[controller]")] public class ValuesController : Controller { // GET api/values [HttpGet] public Dictionary<string,string> Get() { var userPrinciple = User as ClaimsPrincipal; var claims = new Dictionary<string,string>(); foreach (var claim in userPrinciple.Claims) { var key = claim.Type; var value = claim.Value; claims.Add(key,value); } return claims; } 如果我使用具有会计角色的用户登录或具有会计角色的组,则应在地址localhost:57630 / api / values上显示我的用户声明. 我希望这适合你. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – MVC3 TextBoxFor与EditorFor – 每个问题
- ASP.NET Core rc2中的Cookie
- 用户登录时,ASP.NET Core更改EF连接字符串
- asp.net – 使用匈牙利表示法在VB.NET中验证命名样式的工具
- ASP.NET内置用户配置文件与旧stile用户类/表
- asp.net – 组合两个List(Of String)最有效的方法是什么?
- 使用名为“PropertiesController”的控制器的ASP.NET MVC路
- asp.net – 在序列化“System.Reflection”类型的对象时检测
- asp.net-mvc – AJAX POST到MVC Controller显示302错误
- asp.net – 设置值时页面移位 – 我的代码或最新版本的Chro
推荐文章
站长推荐
- asp.net AjaxControlToolKit--TabContainer控件的
- asp.net-mvc – 如何在ASP.NET MVC中定义表单域前
- asp.net-mvc-3 – 如何在Asp.Net MVC中显示Displ
- Asp.Net Core API网关Ocelot
- asp.net-mvc – 允许使用ASP-MVC和表单身份验证访
- asp.net – 从磁条输入信号卡
- asp.net-web-api – 使用ASP.NET WebAPI消费Atla
- asp-classic – Classic ASP中的Response.Flush导
- asp.net-mvc-3 – 获取行中的第一个和第二个td元
- 如何在ASP.NET中绘制折线图
热点阅读