代码之家  ›  专栏  ›  技术社区  ›  K.Z

调用Angular 8应用程序。NET核心Web API通过Azure B2C

  •  0
  • K.Z  · 技术社区  · 5 年前

    我正在开发Angular 8应用程序,它需要从Azure B2C获取令牌,然后调用。NET CORE Web API。我在跟踪 http://about-azure.com/using-azure-ad-b2c-with-angular-8/ 辅导的。我已经为中间件(Web API)创建了Azure应用程序一个,为Angular创建了第二个。

    当我从Angular点击“登录”时,我收到了重定向错误。

     https://localhost/#error=redirect_uri_mismatch&error_description
    

    我相信它在Web API应用程序重定向URL上犯了错误,如下屏幕所示。我按照上述教程中的步骤进行操作

    Azure上的Web API应用程序

    enter image description here

    Azure上的Angular应用程序

    enter image description here

    应用程序组件

    import { Component } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { OAuthService, NullValidationHandler } from 'angular-oauth2-oidc';
    import { authConfig, DiscoveryDocumentConfig } from './auth.config';
    
    @Component({
    selector: 'app-root',
    template: `
    <h1 *ngIf="!claims">
     Hi!
    </h1>
    
    <h1 *ngIf="claims">
     Hi, {{claims.given_name}}!
    </h1>
    
    <h2 *ngIf="claims">Your Claims:</h2>
    
    <pre *ngIf="claims">
    {{claims | json}}
    </pre>
    <br />
    
    <div *ngIf="!claims">
    <button (click)="login()">Login</button>
    </div>
    
    <div *ngIf="claims">
    <button (click)="logout()">Logout</button>
    <button (click)="getMessage()">API Call</button>
    <div *ngIf="message">
      Response:
      {{message | json}}
    </div>
    </div>
    `,
     styles: []
     })
    export class AppComponent {
    constructor(private http: HttpClient, private oauthService: OAuthService) {
     this.configure();
      this.oauthService.tryLoginImplicitFlow();
    }
    
    message: string;
    
    public getMessage() {
     this.http.get("https://localhost:5001/api/values", { responseType: 'text' })
      .subscribe(r => {
        this.message = r
        console.log("message: ", this.message);
      });
    }
    
    public login() {
    
     this.oauthService.initLoginFlow();
    }
    
     public logout() {
      this.oauthService.logOut();
    }
    
    public get claims() {
     let claims = this.oauthService.getIdentityClaims();
     return claims;
    
    }
    
    private configure() {
     this.oauthService.configure(authConfig);
     this.oauthService.tokenValidationHandler = new NullValidationHandler();
     this.oauthService.loadDiscoveryDocument(DiscoveryDocumentConfig.url);
     }
    }
    

    .WEB API部署

    enter code here

    namespace App.Core.API.Controllers
    {
      [Authorize]
      [Route("api/[controller]")]
      [ApiController]
       public class HomeController : ControllerBase
       {
         [HttpGet]
         public IActionResult GetAsync() => Ok("Hello, World");
       }
     }
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   Nan Yu    5 年前

    您应该确认Angular应用程序配置中的重定向url与 http://localhost:4200/index.html ,您可以使用Fiddler或浏览器的开发人员工具来跟踪和查找Azure AD B2C授权请求中的请求url,它必须与您在门户中注册的重定向URI之一完全匹配。