我们先新增一个网站,名为“ClientMvc",也是asp.net core Web应用程序(模型视图控制器)

使用nuget安装以下引用

Microsoft.AspNetCore.Authentication.Cookies

Microsoft.AspNetCore.Authentication.OpenIdConnect

打开Properties\launchSettings.json,修改端口为44302

我们修改该网站的Home页,打开View/Home/Index.cshtml,使用以下内容替换

@using Microsoft.AspNetCore.Authentication

<h2>Claims</h2>

<dl>
@foreach (var claim in User.Claims)
{
<dt>@claim.Type</dt>
<dd>@claim.Value</dd>
}
</dl> <h2>Properties</h2> <dl>
@foreach (var prop in (await Context.AuthenticateAsync()).Properties.Items)
{
<dt>@prop.Key</dt>
<dd>@prop.Value</dd>
}
</dl>

  修改控制器,加上Authorize属性

同样需要调整startup.cs的两个方法

public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews(); JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); IdentityModelEventSource.ShowPII = true; services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
options.Authority = "https://localhost:44300";
options.RequireHttpsMetadata = true;
options.ClientId = "mvc";
options.SaveTokens = true;
});
}

  Configure方法,增加app.UseAuthentication();

MVC的网站调整好了,现在如果运行该网站的话,会提示错误

好了,现在需要去为我们的认证服务器加上Implicit模式的支持

在Config.cs上需修改两处

1.加上相应的Client。

2.添加IdentityResource

以下是整个文件代码

using IdentityServer4;
using IdentityServer4.Models;
using System.Collections.Generic; namespace IdentityMvc
{
public static class Config
{
public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new IdentityResource[]
{
new IdentityResources.OpenId(),
//Implicit need it.
new IdentityResources.Profile()
};
} public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("api1", "My API")
};
} public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client
{
ClientId = "client",
// no interactive user, use the clientid/secret for authentication
AllowedGrantTypes = GrantTypes.ClientCredentials,
// secret for authentication
ClientSecrets =
{
new Secret("secret".Sha256())
},
// scopes that client has access to
AllowedScopes = { "api1" }
},
new Client
{
ClientId = "mvc",
ClientName = "MVC Client",
AllowedGrantTypes = GrantTypes.Implicit,
RequireConsent=false,//不需要显示否同意授权 页面 这里就设置为false
RedirectUris = { "https://localhost:44302/signin-oidc" },//登录成功后返回的客户端地址
PostLogoutRedirectUris = { "https://localhost:44302/signout-callback-oidc" },//注销登录后返回的客户端地址
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email, "api1", "api2.read_only"
},
}
};
}
}
}

  

修改项目为多启动项目

鼠标右键点击”解决方案”,选择属性

按上图启动后,你会发现IE打开两个page,且都访问了44300端口

至此,44302的首页处于认证保护之下了。下一步就是回到44300去实现Account控制器的Login方法,完成整个认证过程。因为要读取数据库,内容比较多,另起一篇来说明过程。

IdentityServer4入门四:应用Implicit模式保护网站(上)的更多相关文章

  1. IdentityServer4入门四:应用Implicit模式保护网站(下)

    为认证服务端增加数据库支持 我计划使用一个名为Admin的表,放在一个已有的数据库里.所以我需要定义Admin类和在配置里预先加上数据库连接 新增类:Admin.cs public class Adm ...

  2. IdentityServer4入门三:授权模式

    在入门一.入门二我们实现了一个完整的API保护的过程.需要保护的API只需在其Controler上应用[Authorize]特性,来显式指定受保护的资源.而我们实现的这个例子,所应用的模式叫“Clie ...

  3. Java设计模式从精通到入门四 工厂方法模式

    工厂方法模式 属于23中设计模式中创建型类型. 核心思想:工厂提供创建对象的接口,由子类决定实例化哪一个子类. 来源 ​ 设计模式之禅中的例子,女娲造人,通过八卦炉来进行造人,没有烧熟的为白人,烧太熟 ...

  4. 大型网站技术架构(四)--核心架构要素 开启mac上印象笔记的代码块 大型网站技术架构(三)--架构模式 JDK8 stream toMap() java.lang.IllegalStateException: Duplicate key异常解决(key重复)

    大型网站技术架构(四)--核心架构要素   作者:13GitHub:https://github.com/ZHENFENG13版权声明:本文为原创文章,未经允许不得转载.此篇已收录至<大型网站技 ...

  5. IdentityServer4 中文文档 -9- (快速入门)使用客户端凭证保护API

    IdentityServer4 中文文档 -9- (快速入门)使用客户端凭证保护API 原文:http://docs.identityserver.io/en/release/quickstarts/ ...

  6. 解析汽车B2C商城网站四种盈利模式

    汽车已成为家庭的日常用品,汽车的配套设施也成为销售的热点,汽车B2C电子商城为行业营销的新平台,汽车B2C电子商务网站盈利的模式是怎样的?创新的盈利模式才能在行业竞争中生存. 资讯产品一体模式 网站的 ...

  7. 微服务(入门四):identityServer的简单使用(客户端授权)

    IdentityServer简介(摘自Identity官网) IdentityServer是将符合规范的OpenID Connect和OAuth 2.0端点添加到任意ASP.NET核心应用程序的中间件 ...

  8. IdentityServer4 实现自定义 GrantType 授权模式

    OAuth 2.0 默认四种授权模式(GrantType): 授权码模式(authorization_code) 简化模式(implicit) 密码模式(password) 客户端模式(client_ ...

  9. 从Client应用场景介绍IdentityServer4(四)

    原文:从Client应用场景介绍IdentityServer4(四) 上节以对话形式,大概说了几种客户端授权模式的原理,这节重点介绍Hybrid模式在MVC下的使用.且为实现IdentityServe ...

随机推荐

  1. 移动端调试工具Vconsole

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. Java中的File操作总结

    1.创建文件 import java.io.File; import java.io.IOException; public class CreateFileExample { public stat ...

  3. ubuntu 中安装sublime-text3

    ubuntu 中安装sublime_text3Enter "Alt+m" will show Markdown Preview 安装 输入注册码 汉化 安装插件 中文输入bug修复 ...

  4. 助教培训总结——熟练掌握GitHub及Git的使用方法

    一.Git 命令的理解和使用 1.使用git前需要建立一个本地仓库,用Git GUI Here的话就可以直接选择Create New Repository.Git Bash Here输入 命令git ...

  5. 【搜索-剪枝-偏难】PAT-天梯赛-L3-015. 球队“食物链”

    L3-015. 球队“食物链” 某国的足球联赛中有N支参赛球队,编号从1至N.联赛采用主客场双循环赛制,参赛球队两两之间在双方主场各赛一场. 联赛战罢,结果已经尘埃落定.此时,联赛主席突发奇想,希望从 ...

  6. P2002 消息扩散[SCC缩点]

    题目描述 有n个城市,中间有单向道路连接,消息会沿着道路扩散,现在给出n个城市及其之间的道路,问至少需要在几个城市发布消息才能让这所有n个城市都得到消息. 输入格式 第一行两个整数n,m表示n个城市, ...

  7. ASP.NET 下配置请求大小、请求时间等参数

    在邮件发送系统或者其他一些传送文件的网站中,用户传送文件的大小是有限制的,因为这样不但可以节省服务器的空间,还可以提高传送文件的速度.下面介绍如何在Web.Config文件中配置限制上传文件大小与时间 ...

  8. 前端学习笔记--Visual Studio Code安装及中文显示

    1.在官网https://code.visualstudio.com/下载对应的版本: 2.安装 一路点击下一步,选中  添加到PATH后,安装. 安装成功,可以直接打开使用: 把界面改成中文显示: ...

  9. 16 关于webpack和npm中几个问题的说明

    1.json里面不能写注释 2.'webpack-dev-server'不是内部或外部命令,也不是可运行的程序或批处理文件. 注意:webpack-dev-server包只需要本地安装就行,不用全局安 ...

  10. 云计算(3)-what is new in today's Cloud

    What is new in today's Cloud Four features new in today's Clouds 如果一个problem有以上4个features里面的一个或者多个,则 ...