使用Ionic 2配置Identity Server 4

约翰尼5

我试图配置Identity Server与Ionic 2一起使用。我对如何配置重定向URL感到有些困惑。当我在浏览器中测试时。

我正在更新和集成OIDC Cordova组件。旧的组件git hub在这里:https : //github.com/markphillips100/oidc-cordova-demo

我创建了一个打字稿提供程序并将其注册到我的app.module.ts中

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import { Component } from '@angular/core';
import * as Oidc from "oidc-client";
import { Events } from 'ionic-angular';
import { environment } from "../rules/environments/environment";

export class UserInfo {
    user: Oidc.User = null;
    isAuthenticated: boolean = false;
}

@Injectable()
export class OidcClientProvider   {

    USERINFO_CHANGED_EVENT_NAME: string = ""
    userManager: Oidc.UserManager;
    settings: Oidc.UserManagerSettings;
    userInfo: UserInfo = new UserInfo();
    constructor(public events:Events) {

        this.settings = {
            //authority: "https://localhost:6666",
            authority: environment.identityServerUrl,
            client_id: environment.clientAuthorityId,
            //This doesn't work
            post_logout_redirect_uri: "http://localhost/oidc",
            redirect_uri: "http://localhost/oidc",
            response_type: "id_token token",
            scope: "openid profile",

            automaticSilentRenew: true,
            filterProtocolClaims: true,
            loadUserInfo: true,
            //popupNavigator: new Oidc.CordovaPopupNavigator(),
            //iframeNavigator: new Oidc.CordovaIFrameNavigator(),
        }

        this.initialize();
    }

    userInfoChanged(callback: Function) {
        this.events.subscribe(this.USERINFO_CHANGED_EVENT_NAME, callback);
    }

    signinPopup(args?): Promise<Oidc.User> {
        return this.userManager.signinPopup(args);
    }

    signoutPopup(args?) {
        return this.userManager.signoutPopup(args);
    }

    protected initialize() {

        if (this.settings == null) {
            throw Error('OidcClientProvider required UserMangerSettings for initialization')
        }

        this.userManager = new Oidc.UserManager(this.settings);
        this.registerEvents();
    }

    protected notifyUserInfoChangedEvent() {
        this.events.publish(this.USERINFO_CHANGED_EVENT_NAME);
    }

    protected clearUser() {
        this.userInfo.user = null;
        this.userInfo.isAuthenticated = false;
        this.notifyUserInfoChangedEvent();
    }

    protected addUser(user: Oidc.User) {
        this.userInfo.user = user;
        this.userInfo.isAuthenticated = true;
        this.notifyUserInfoChangedEvent();
    }

    protected registerEvents() {
        this.userManager.events.addUserLoaded(u => {
            this.addUser(u);
        });

        this.userManager.events.addUserUnloaded(() => {
            this.clearUser();
        });

        this.userManager.events.addAccessTokenExpired(() => {
            this.clearUser();
        });

        this.userManager.events.addSilentRenewError(() => {
            this.clearUser();
        });
    }
}

我试图了解如何配置重定向URL,以便可以在浏览器中正常进行身份验证。通常,您将配置重定向URL,以使您的进程在登录后获得令牌和声明。

this.settings = {
        authority: environment.identityServerUrl,
        client_id: environment.clientAuthorityId,
        post_logout_redirect_uri: "http://localhost:8100/oidc",
        redirect_uri: "http://localhost:8100/oidc",
        response_type: "id_token token",
        scope: "openid profile AstootApi",

        automaticSilentRenew: true,
        filterProtocolClaims: true,
        loadUserInfo: true,
        //popupNavigator: new Oidc.CordovaPopupNavigator(),
        //iframeNavigator: new Oidc.CordovaIFrameNavigator(),
    }

Ionic 2不使用url进行路由,假设我有一个AuthenticationPage处理身份验证令牌的组件如何配置重定向URL,以便它导航到身份验证页面,以便可以在浏览器中进行测试?

约翰尼5

TL; DR

我必须做一些事情才能使它正常工作。
起初我没有意识到,但是我的重定向URL必须与我的客户端在身份服务器中存储的内容匹配。

new Client
{
    ClientId = "myApp",
    ClientName = "app client",
    AccessTokenType = AccessTokenType.Jwt,
    RedirectUris = { "http://localhost:8166/" },
    PostLogoutRedirectUris = { "http://localhost:8166/" },
    AllowedCorsOrigins = { "http://localhost:8166" },
    //...
}

因此,Typescript中的OIDC客户端也需要更新。

this.settings = {
    authority: environment.identityServerUrl,
    client_id: environment.clientAuthorityId,
    post_logout_redirect_uri: "http://localhost:8166/",
    redirect_uri: "http://localhost:8166/",
    response_type: "id_token token",
}

另外,由于我不想在Ionic中设置路由,因此我需要找出一种与Ionic进行通信的url方式(出于浏览器测试的目的,正常的通信将通过cordova进行)。

因此,我指出了重定向网址是托管我的应用程序的URL,并且在构造函数中的app.Component.ts上添加了代码以尝试获取身份验证令牌。

constructor(
  public platform: Platform,
  public menu: MenuController,
  public oidcClient: OidcClientProvider
)
{
  //Hack: since Ionic only has 1 default address, attempt to verify if this is a call back before calling 
   this.authManager.verifyLoginCallback().then((isSuccessful) => {
     if (!isSuccessful) {
        this.authManager.IsLoggedIn().then((isLoggedIn) => {
          if (isLoggedIn) {
              return;
          }

          this.nav.setRoot(LoginComponent)
        });
     }
  });
}

编辑验证登录回叫应该只是oidc客户端回叫,它将从get参数读取令牌

verifyLoginCallback(): Promise<boolean> {
    return this.oidcClient.userManager.signinPopupCallback()
        .then(user => {
            return this.loginSuccess(user).
                then(() => true,
                    () => false);
    }, err => { console.log(err); return false; });
} 

注意Login组件只是代表登录登录页面的模式,仅使用登录按钮来初始化弹出窗口。您可以将其挂接到任何由用户驱动的事件中,以触发登录,但是如果要支持Web而又不触发弹出窗口阻止程序,则必须使用用户驱动的事件。

<ion-footer no-shadow>
  <ion-toolbar no-shadow position="bottom">
    <button ion-button block (click)="login()">Login</button>
  </ion-toolbar>
</ion-footer>

login(): Promise<any> {
    return this.oidcClient.signinPopup().then((user) => {
        this.events.publish(environment.events.loginSuccess);
    }).catch((e) => { console.log(e); });
}

我确信最好将重定向重定向到其他路由,这只是一个快速而肮脏的技巧

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

配置 ASP.NET MVC 4 Web 应用程序以使用 Identity Server 4

Identity Server 4 AddOidcStateDataFormatterCache配置TimeToLive

Identity Server 4:使用EntityFramework Core进行配置和操作数据

在 Identity Server 4 中使用 fetch

Identity Server 4 Angular 2令牌到期

如何配置 WSO2 API Manager 2.1.0 以使用 Identity Server 5.4.1

使用Identity Server 4隐式流程在ASP.net核心API中获取用户配置文件

Asp.net Core 2使用Identity Server 4启用多租户

角色-Identity Server 4

Identity Server 4 with WebAuthN - 使用 GrantType (FIDO 2.0)

REST API 和 Identity Server 4 使用 Postman 进行测试

如何使用 iFrame 在隐式流中手动进行静默刷新(使用 Identity Server 4、Angular 2+)

使用angular-oauth2-oidc和Identity Server 4时无法获取用户名/给定名称

配置Identity Server以使用ASP.NET身份角色

安装 Identity Server 4 模板

Identity Server 4 / nativescript挂起

Identity Server 4 Cors问题

Identity Server 4和Docker

Identity Server 4无限循环

Identity Server 4 + Identity Framework + React前端

ASP.NET 3.1 中 Identity Server 4 中的 ApiResources 配置在哪里?

扩展Identity Server 4的配置和操作数据上下文

Identity Server4:无法从“ http:// localhost:44338 / .well-known / openid-configuration”获取配置

使用 Identity Server 3 和 AspNet Identity 为项目配置 IUserTokenProvider

如何使用ionic 2 RC 4本机网络

如何使用Identity Server 4中的C#正确获取令牌以在Postman中使用?

Identity Server 4从Db撤消访问令牌

对Identity Server 4进行故障排除

Identity Server4未经授权的错误