Nestjs-实施Azure -AD护照身份验证

发布于 2025-02-11 17:48:55 字数 44 浏览 1 评论 0原文

如何实施Azure-AD护照身份验证?找不到任何文档,并在线阅读存在问题。

How can I implement Azure-Ad Passport Authentication? Can't find any documentation for it, and read online that there are problems with that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小女人ら 2025-02-18 17:48:56

使用MSAL进行前端。
用于后端使用护照和 passport-azure-ad npm。

  • 然后,您应该在Azure AD中创建应用程序
  • ,然后获取tenantid和
    然后,从应用程序设置页面上的Appid,
  • 然后使用这样的代码获取访问权限
    令牌并检查用户验证。
// my auth-guard.ts

import { AuthGuard, PassportStrategy } from '@nestjs/passport';
import { BearerStrategy } from 'passport-azure-ad';
import { Injectable } from '@nestjs/common';


@Injectable()
export class AzureADStrategy extends PassportStrategy(
  BearerStrategy,
  'azure-ad',
) {
  constructor() {
    super({
      identityMetadata: `https://login.microsoftonline.com/${tenantID}/v2.0/.well-known/openid-configuration`,
      clientID,
    });
  }

  async validate(data) {
    return data;
  }
}

export const AzureADGuard = AuthGuard('azure-ad');

// app.controller.ts

  @UseGuards(AzureADGuard)
  @Get('/api')
  get_api(): string {
    return 'OK';
  }
}

尝试它应该起作用。

Use MSAL for FrontEnd.
For Backend use Passport and passport-azure-ad npm.
enter image description here

  • Then, you should create your app in Azure AD
  • then, get tenantId and
    appId from app settings page,
  • then, use code like that to get access
    token and check user auth.
// my auth-guard.ts

import { AuthGuard, PassportStrategy } from '@nestjs/passport';
import { BearerStrategy } from 'passport-azure-ad';
import { Injectable } from '@nestjs/common';


@Injectable()
export class AzureADStrategy extends PassportStrategy(
  BearerStrategy,
  'azure-ad',
) {
  constructor() {
    super({
      identityMetadata: `https://login.microsoftonline.com/${tenantID}/v2.0/.well-known/openid-configuration`,
      clientID,
    });
  }

  async validate(data) {
    return data;
  }
}

export const AzureADGuard = AuthGuard('azure-ad');

// app.controller.ts

  @UseGuards(AzureADGuard)
  @Get('/api')
  get_api(): string {
    return 'OK';
  }
}

try it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文