我正在开发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应用程序
Azure上的Angular应用程序
应用程序组件
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部署
namespace App.Core.API.Controllers
{
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class HomeController : ControllerBase
{
[HttpGet]
public IActionResult GetAsync() => Ok("Hello, World");
}
}