首页 > 其他 > 详细

基于IdentityServer4的声明的授权

时间:2017-11-12 10:17:45      阅读:497      评论:0      收藏:0      [点我收藏+]

## 概述

基于Asp.net Core 1.1 ,使用IdentityServer4认证与授权。

## 参考资料

[微软教程](https://docs.microsoft.com/zh-cn/aspnet/core/security/authorization/claims#security-authorization-claims-based)

## 客户端的设置

### 配置与IdentityServerSystem连接

//配置与IdentityServerSystem连接
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookies"
});
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();


////For MVC Implicit Flow
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AuthenticationScheme = "oidc",
SignInScheme = "Cookies",
Authority = "http://localhost:5000",
ClientId = "testClientID",
RequireHttpsMetadata = false,
Scope = { "testclientidentity" },
GetClaimsFromUserInfoEndpoint = true,

SaveTokens = true
});

### 在Controller的Action中加入权限

using Microsoft.AspNetCore.Authorization;
public class TestsController : Controller
 [Authorize(Policy = "EditInfo")]
  public IActionResult Edit(){
  }
}

 

## IdentityServer4服务器端设置

### 创建ClientID

ClientID为testClientID,

### 创建IdentityScope

创建名为testclientidentity的IdentityScope

在其中加入一些UserClaim,比如"EditInfo"

## 测试

1. 利用用户名为"a"的用户登陆程序,在进入Tests/Edit时,会提示拒绝访问的错误界面

2. 给用户"a"加入名为"EditInfo"的UserClaim,再次进入Tests/Edit时,会正确显示界面

基于IdentityServer4的声明的授权

原文:http://www.cnblogs.com/hahaxi/p/7820509.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!