简体   繁体   中英

Call Angular 8 App to .NET Core Web API via Azure B2C

I am working on Angular 8 application that requires to get token from Azure B2C, followed by call .NET CORE Web API. I am following http://about-azure.com/using-azure-ad-b2c-with-angular-8/ Tutorial. I have create Azure Application one for middleware (Web API) and second one for Angular.

I am getting redirect error when I click on 'login' from Angular.

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

I believe it did mistake on Web API Application Redirect URL as shown in following screen. I have follow above steps from the above tutorial

Web API Application On Azure

在此处输入图片说明

Angular Application On Azure

在此处输入图片说明

App-Component

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 deployment

在此处输入代码

namespace App.Core.API.Controllers
{
  [Authorize]
  [Route("api/[controller]")]
  [ApiController]
   public class HomeController : ControllerBase
   {
     [HttpGet]
     public IActionResult GetAsync() => Ok("Hello, World");
   }
 }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM