基于IdentityServer4的声明的授权

## 概述

基于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时,会正确显示界面

posted @ 2017-11-12 00:54  哈哈兮  阅读(1019)  评论(0编辑  收藏  举报