参照 草根专栏- ASP.NET Core + Ng6 实战:https://v.qq.com/x/page/i07702h18nz.html

1、 OIDC-Client

https://github.com/IdentityModel/oidc-client-js

npm install oidc-client --save

2、建立   open-id-connect.service.ts

ng g s shared/oidc/openIdConnect

3、 environment,配置oidc客户端

export const environment = {
production: false ,
apiUrlBase: '/api',
openIdConnectSettings: {
authority: 'https://localhost:5001/',
client_id: 'blog-client',
redirect_uri: 'http://localhost:4200/signin-oidc',
scope: 'openid profile email restapi',
response_type: 'id_token token',
post_logout_redirect_uri: 'http://localhost:4200/',
automaticSilentRenew: true,
silent_redirect_uri: 'http://localhost:4200/redirect-silentrenew'
}
};

4、BlogIdp项目,config.cs 文件配置客户端访问:

                // Angular client using implicit flow
new Client
{
ClientId = "blog-client",
ClientName = "Blog Client",
ClientUri = "http://localhost:4200", AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser = true,
RequireConsent = false,
AccessTokenLifetime = , RedirectUris =
{
"http://localhost:4200/signin-oidc",
"http://localhost:4200/redirect-silentrenew"
}, PostLogoutRedirectUris = { "http://localhost:4200/" },
AllowedCorsOrigins = { "http://localhost:4200" }, AllowedScopes = {
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
"restapi" }
}

5、Blogidp项目配置跨域:

            services.AddCors(options =>
{
options.AddPolicy("AngularDev", policy =>
{
policy.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
        public void Configure(IApplicationBuilder app)
{ app.UseCors(); }

6、open-id-connect.service.ts 添加登陆操作:

import { Injectable } from '@angular/core';
import { UserManager, User } from 'oidc-client';
import { environment } from '../../../environments/environment';
import { ReplaySubject } from 'rxjs'; @Injectable({
providedIn: 'root'
})
export class OpenIdConnectService { private userManager: UserManager = new UserManager(environment.openIdConnectSettings); private currentUser: User; userLoaded$ = new ReplaySubject<boolean>(); get userAvailable(): boolean {
return this.currentUser != null;
} get user(): User {
return this.currentUser;
} constructor() {
this.userManager.clearStaleState(); this.userManager.events.addUserLoaded(user => {
if (!environment.production) {
console.log('User loaded.', user);
}
this.currentUser = user;
this.userLoaded$.next(true);
}); this.userManager.events.addUserUnloaded((e) => {
if (!environment.production) {
console.log('User unloaded');
}
this.currentUser = null;
this.userLoaded$.next(false);
});
} triggerSignIn() {
this.userManager.signinRedirect().then(() => {
if (!environment.production) {
console.log('Redirection to signin triggered.');
}
});
} handleCallback() {
this.userManager.signinRedirectCallback().then(user => {
if (!environment.production) {
console.log('Callback after signin handled.', user);
}
});
} handleSilentCallback() {
this.userManager.signinSilentCallback().then(user => {
this.currentUser = user;
if (!environment.production) {
console.log('Callback after silent signin handled.', user);
}
});
} triggerSignOut() {
this.userManager.signoutRedirect().then(resp => {
if (!environment.production) {
console.log('Redirection to sign out triggered.', resp);
}
});
} }

7、添加  signin-oidc component

8、添加 redirect-silent-renew component

9、 ng g guard shared/oidc/RequireAuthenticatedUserRoute --spec false

触发登陆界面;

app.module  Provided  添加   RequireAuthenticatedUserRouteGuard , OpenIdConnectService

10、blog-routing.module.ts 注册路由:

const routes: Routes = [
{
path: '', component: BlogAppComponent,
children : [
{path: 'post-list' , component: PostListComponent, canActivate: [RequireAuthenticatedUserRouteGuard] },
{path: '**' , redirectTo: 'post-list' }
]
}

11、配置登陆路由: app.module

const routers: Routes = [
{ path: 'blog', loadChildren: './blog/blog.module#BlogModule' },
{ path: 'signin-oidc', component: SigninOidcComponent },
{ path: 'redirect-silentrenew', component: RedirectSilentRenewComponent },
{ path: '**', redirectTo: 'blog' }
];

12、添加拦截器:authorization-header-interceptor.interceptor.ts  ,在blog.module.ts 里面配置

  providers: [
PostService,
{
provide: HTTP_INTERCEPTORS,
useClass: AuthorizationHeaderInterceptor,
multi: true
}
]

OIDC in Angular 6的更多相关文章

  1. [OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)

    在上一篇基于OIDC的SSO的中涉及到了4个Web站点: oidc-server.dev:利用oidc实现的统一认证和授权中心,SSO站点. oidc-client-hybrid.dev:oidc的一 ...

  2. angular 接入 IdentityServer4

    angular 接入 IdentityServer4 Intro 最近把活动室预约的项目做了一个升级,预约活动室需要登录才能预约,并用 IdentityServer4 做了一个统一的登录注册中心,这样 ...

  3. Angular杂谈系列1-如何在Angular2中使用jQuery及其插件

    jQuery,让我们对dom的操作更加便捷.由于其易用性和可扩展性,jQuer也迅速风靡全球,各种插件也是目不暇接. 我相信很多人并不能直接远离jQuery去做前端,因为它太好用了,我们以前做的东西大 ...

  4. Angular企业级开发(5)-项目框架搭建

    1.AngularJS Seed项目目录结构 AngularJS官方网站提供了一个angular-phonecat项目,另外一个就是Angular-Seed项目.所以大多数团队会基于Angular-S ...

  5. TypeScript: Angular 2 的秘密武器(译)

    本文整理自Dan Wahlin在ng-conf上的talk.原视频地址: https://www.youtube.com/watch?v=e3djIqAGqZo 开场白 开场白主要分为三部分: 感谢了 ...

  6. angular实现统一的消息服务

    后台API返回的消息怎么显示更优雅,怎么处理才更简洁?看看这个效果怎么样? 自定义指令和服务实现 自定义指令和服务实现消息自动显示在页面的顶部,3秒之后消失 1. 显示消息 这种显示消息的方式是不是有 ...

  7. div实现自适应高度的textarea,实现angular双向绑定

    相信不少同学模拟过腾讯的QQ做一个聊天应用,至少我是其中一个. 过程中我遇到的一个问题就是QQ输入框,自适应高度,最高高度为3row. 如果你也像我一样打算使用textarea,那么很抱歉,你一开始就 ...

  8. Angular企业级开发-AngularJS1.x学习路径

    博客目录 有链接的表明已经完成了,其他的正在建设中. 1.AngularJS简介 2.搭建Angular开发环境 3.Angular MVC实现 4.[Angular项目目录结构] 5.[SPA介绍] ...

  9. Angular企业级开发(4)-ngResource和REST介绍

    一.RESTful介绍 RESTful维基百科 REST(表征性状态传输,Representational State Transfer)是Roy Fielding博士在2000年他的博士论文中提出来 ...

随机推荐

  1. init/loadView/viewDidLoad/initWithNibName/awakeFromNib/initWithCoder的用法

    init/loadView/viewDidLoad/viewDidUnload 这么细节的东西想来大家都不在意,平时也不会去关系,但是在面试时却常常被提到,所以了解viewController的生命周 ...

  2. hadoop-2.2.0 HA配置

    采用的是4台真实机器: namenode:qzhong  node27 datanode:qzhong node27 node100 node101 操作系统环境:qzhong(Ubuntu-14.0 ...

  3. 【HDOJ 1337】I Hate It(线段树维护区间最大值)

    Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写 ...

  4. window下使用Composer安装yii2

    1.在 Windows 中,先下载并运行 Composer-Setup.exe,安装过程需选择php的运行目录,安装完后在windows的cmd下运行composer看看是否安装成功 2.安装完Com ...

  5. Eclipse易卡死

    在用eclipse编辑项目的时候,经常卡死,经过查询知道原来是我的JDK和eclipse版本对应的不好,我们都知道,eclipse的环境需要配置. 当时情况是这样的 2.容易出现卡死或者如图所示的情况 ...

  6. 六个比较好用的php数组Array函数

    1. array_column 返回输入数组中某个单一列的值.2. array_filter 用回调函数过滤数组中的元素.3. array_map 将用户自定义函数作用到给定数组的每个值上,返回新的值 ...

  7. CentOS7 LNMP+phpmyadmin环境搭建(二、LNMP环境搭建)

    上一篇博客我们在虚拟机上安装了centos7,接下来,就开始安装lnmp环境吧. 还是跟之前一样,进入命令行后,先使用su命令切换到root权限. 首先配置防火墙  CentOS 7.0默认使用的是f ...

  8. Hbase系统架构简述

    由于最近要开始深入的学习一下hbase,所以,先大概了解了hbase的基本架构,在此简单的记录一下. Hbase的逻辑视图 Hbase的物理存储 HRegion Table中所有行都按照row key ...

  9. 用树莓派3B+和 ITEAD PN532 读取、破解、写入M1卡

    这是一篇介绍如何用树莓派使用PN532的随笔,介绍了具体的使用步骤. 首先介绍一下: ①.IC卡是非接触式的智能卡,里面一般是一个方形线圈和一个小芯片(用强光照着可以看到).M1卡是IC卡的一种,一般 ...

  10. apache的.htaccess规则

    1..htaccess文件使用前提 .htaccess的主要作用就是实现url改写,也就是当浏览器通过url访问到服务器某个文件夹时,作为主人,我们可以来接待这个url,具体 地怎样接待它,就是此文件 ...