Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Dotnet core 3.1和Angular 9的AAD注册_C#_Angular_Azure_Azure Active Directory_Asp.net Core Webapi - Fatal编程技术网

C# Dotnet core 3.1和Angular 9的AAD注册

C# Dotnet core 3.1和Angular 9的AAD注册,c#,angular,azure,azure-active-directory,asp.net-core-webapi,C#,Angular,Azure,Azure Active Directory,Asp.net Core Webapi,我需要构建一个简单的spa(angular 9)应用程序,在Azure Active Directory注册的基础上使用dotnet core 3.1。是否有文档或教程说明如何使dotnet core+angular+AAD auth成为简单的应用程序 我已经找到了这篇文章,但它似乎不适合今天 我注册了两个应用程序注册(如文章所示),并接受了测试 我的app.module.ts包含: function MSALConfigFactory(): Configuration { return {

我需要构建一个简单的spa(angular 9)应用程序,在Azure Active Directory注册的基础上使用dotnet core 3.1。是否有文档或教程说明如何使dotnet core+angular+AAD auth成为简单的应用程序

我已经找到了这篇文章,但它似乎不适合今天

我注册了两个应用程序注册(如文章所示),并接受了测试

我的app.module.ts包含:

function MSALConfigFactory(): Configuration {
  return {
    auth: {
      clientId: '<client-id-of-frontend-app-registration>',
      authority: "https://login.microsoftonline.com/<tenant-id>",
      validateAuthority: true
      // redirectUri: "http://localhost:4200/",
      // postLogoutRedirectUri: "http://localhost:4200/",
      // navigateToLoginRequestUrl: true,
    },
    cache: {
      cacheLocation: "localStorage",
      storeAuthStateInCookie: isIE, // set to true for IE 11
    },
  };
}
{
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/ ",
    "Domain": "microsoft.onmicrosoft.com",
    "TenantId": "<tenant-id>",
    "ClientId": "<client-id-of-backend-app-registration>"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}
因此,appsettings.json包含:

function MSALConfigFactory(): Configuration {
  return {
    auth: {
      clientId: '<client-id-of-frontend-app-registration>',
      authority: "https://login.microsoftonline.com/<tenant-id>",
      validateAuthority: true
      // redirectUri: "http://localhost:4200/",
      // postLogoutRedirectUri: "http://localhost:4200/",
      // navigateToLoginRequestUrl: true,
    },
    cache: {
      cacheLocation: "localStorage",
      storeAuthStateInCookie: isIE, // set to true for IE 11
    },
  };
}
{
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/ ",
    "Domain": "microsoft.onmicrosoft.com",
    "TenantId": "<tenant-id>",
    "ClientId": "<client-id-of-backend-app-registration>"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}
{
“蓝精灵”:{
“实例”:https://login.microsoftonline.com/ ",
“域”:“microsoft.onmicrosoft.com”,
“租户”:“,
“客户端ID”:”
},
“日志记录”:{
“日志级别”:{
“默认值”:“信息”,
“Microsoft”:“警告”,
“Microsoft.Hosting.Lifetime”:“信息”
}
},
“AllowedHosts”:“*”
}
当我运行该项目时,我单击“登录”,一切都正常,然后通过注入的HTTPClients的所有请求都包含承载令牌

但当我调用任何标有[Authorize]的控制器时,它总是返回401

那么,也许有些步骤包含错误?或者是否有文档或教程,说明如何使dotnetcore+angular+AAD auth成为一个简单的应用程序?至少对于dotnet和angular的另一个版本来说是这样,但也不太旧


谢谢。

您应该在Angular应用程序中获取访问令牌以访问您的web api应用程序

注册web api应用程序时,配置
公开api
添加作用域
,如:
api:///api-access
,并在.net核心web应用程序中,将
ClientId
设置为
api://

在应用程序端,您可以设置
同意范围
,以包括web api的范围:

同意范围:[”api:///api-access“]

  • 同意范围
    :允许客户端表示应同意的所需范围。作用域可以来自多个资源/端点。在这里传递作用域只会同意它,并且在客户端实际调用API之前不会获取访问令牌
并将
protectedResourceMap
设置为包括获取访问令牌的api范围:

  • protectedResourceMap
    :将资源映射到作用域{“,[“user.read”,“mail.send”]}。MSAL在内部用于在webApi调用中自动附加令牌。这仅适用于CORS呼叫
例如:
export const protectedResourceMap:[string,string[]][]=[[]https://localhost:44388/api/values', ['api://59b02905-8b6b-4665-a702-321e97392416/api-access']] ];

您可以查看以了解更多详细信息。这是给9号的。您可以通过更新app.module.ts中的配置来修改代码示例

更新:

您正在使用Azure AD V2.0,因此在验证web api中的令牌时,管理局应添加
/V2.0

services.AddAuthentication(AzureADDefaults.JwtBearerAuthenticationScheme)
.AddAzureADBearer(options => Configuration.Bind("AzureAd", options));

services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, options =>
{
    // This is a Microsoft identity platform web API.
    options.Authority += "/v2.0";

    // The web API accepts as audiences both the Client ID (options.Audience) and api://{ClientID}.
    options.TokenValidationParameters.ValidAudiences = new []
    {
     options.Audience,
     $"api://{options.Audience}"
    };


}); 
services.AddAuthentication(AzureADDefaults.JwtBearerAuthenticationScheme)
.AddAzureADBearer(options=>Configuration.Bind(“AzureAd”,options));
Configure(AzureADDefaults.JwtBearerAuthenticationScheme,选项=>
{
//这是一个Microsoft identity platform web API。
选项。权限+=“/v2.0”;
//web API接受客户机ID(options.acquisition)和API://{ClientID}作为访问群体。
options.TokenValidationParameters.Validudiences=new[]
{
选择,观众,
$“api://{options.viewer}”
};
}); 

您应该在Angular应用程序中获取访问令牌以访问您的web api应用程序

注册web api应用程序时,配置
公开api
添加作用域
,如:
api:///api-access
,并在.net核心web应用程序中,将
ClientId
设置为
api://

在应用程序端,您可以设置
同意范围
,以包括web api的范围:

同意范围:[”api:///api-access“]

  • 同意范围
    :允许客户端表示应同意的所需范围。作用域可以来自多个资源/端点。在这里传递作用域只会同意它,并且在客户端实际调用API之前不会获取访问令牌
并将
protectedResourceMap
设置为包括获取访问令牌的api范围:

  • protectedResourceMap
    :将资源映射到作用域{“,[“user.read”,“mail.send”]}。MSAL在内部用于在webApi调用中自动附加令牌。这仅适用于CORS呼叫
例如:
export const protectedResourceMap:[string,string[]][]=[[]https://localhost:44388/api/values', ['api://59b02905-8b6b-4665-a702-321e97392416/api-access']] ];

您可以查看以了解更多详细信息。这是给9号的。您可以通过更新app.module.ts中的配置来修改代码示例

更新:

您正在使用Azure AD V2.0,因此在验证web api中的令牌时,管理局应添加
/V2.0

services.AddAuthentication(AzureADDefaults.JwtBearerAuthenticationScheme)
.AddAzureADBearer(options => Configuration.Bind("AzureAd", options));

services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, options =>
{
    // This is a Microsoft identity platform web API.
    options.Authority += "/v2.0";

    // The web API accepts as audiences both the Client ID (options.Audience) and api://{ClientID}.
    options.TokenValidationParameters.ValidAudiences = new []
    {
     options.Audience,
     $"api://{options.Audience}"
    };


}); 
services.AddAuthentication(AzureADDefaults.JwtBearerAuthenticationScheme)
.AddAzureADBearer(options=>Configuration.Bind(“AzureAd”,options));
Configure(AzureADDefaults.JwtBearerAuthenticationScheme,选项=>
{
//这是一个Microsoft identity platform web API。
选项。权限+=“/v2.0”;
//web API接受客户机ID(options.acquisition)和API://{ClientID}作为访问群体。
options.TokenValidationParameters.Validudiences=new[]
{
选择,观众,
$“api://{options.viewer}”
};
}); 

这是为寻求完整实施的人准备的

这是为寻求全面实施的人准备的

谢谢您的回复!请注意,我使用的是相同的angular-9示例(提供