要解决ADFS身份验证ASP.NET Core + Angular没有OAuth的错误,可以遵循以下步骤:
services.AddAuthentication()
.AddWsFederation(options =>
{
options.MetadataAddress = "https://your-adfs-server/federationmetadata/2007-06/federationmetadata.xml";
options.Wtrealm = "urn:your-app-realm";
});
export const adfsConfig = {
authority: 'https://your-adfs-server/adfs',
client_id: 'your-client-id',
redirect_uri: 'http://localhost:4200/login-callback',
post_logout_redirect_uri: 'http://localhost:4200/',
response_type: 'id_token token',
scope: 'openid',
filterProtocolClaims: true,
loadUserInfo: true,
};
import { Injectable } from '@angular/core';
import { UserManager, UserManagerSettings } from 'oidc-client';
import { adfsConfig } from './auth-config';
@Injectable({
providedIn: 'root',
})
export class AuthService {
private userManager: UserManager;
constructor() {
const settings: UserManagerSettings = {
authority: adfsConfig.authority,
client_id: adfsConfig.client_id,
redirect_uri: adfsConfig.redirect_uri,
post_logout_redirect_uri: adfsConfig.post_logout_redirect_uri,
response_type: adfsConfig.response_type,
scope: adfsConfig.scope,
filterProtocolClaims: adfsConfig.filterProtocolClaims,
loadUserInfo: adfsConfig.loadUserInfo,
};
this.userManager = new UserManager(settings);
}
login() {
return this.userManager.signinRedirect();
}
logout() {
return this.userManager.signoutRedirect();
}
completeLogin() {
return this.userManager.signinRedirectCallback();
}
completeLogout() {
return this.userManager.signoutRedirectCallback();
}
getUser() {
return this.userManager.getUser();
}
}
import { Component } from '@angular/core';
import { AuthService } from '../auth/auth.service';
@Component({
selector: 'app-login',
template: `
`,
})
export class LoginComponent {
constructor(private authService: AuthService) {}
login() {
this.authService.login();
}
}
在回调组件的构造函数中添加以下代码:
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../auth/auth.service';
@Component({
selector: 'app-login-callback',
template: '',
})
export class LoginCallbackComponent implements OnInit {
constructor(private authService: AuthService) {}
ngOnInit() {
this.authService.completeLogin().then(() => {
// 处理登录完成后的逻辑
});
}
}
[Authorize]
public class MyController : Controller
{
// 控制器方法
}
以上步骤将帮助你在ASP.NET Core和Angular应用中实现ADFS身份验证,解决没有OAuth的问题。请根据你的实际情况调整配置和代码。