关键词:
【中文标题】如何在 ASP.NET Core 3.1 中启用多重身份验证?【英文标题】:How to enable multiple authentication in ASP.NET Core 3.1? 【发布时间】:2020-08-31 01:32:49 【问题描述】:在我的 aspnet core 3.1 项目中,我使用 jwt 进行身份验证。问题是我也在使用 azure 客户端来获取 vm 大小名称列表,并且它也在使用 Bearer 令牌。现在测试我使用 azure 中的 AllowAnonymous 和 Bearer 一切正常,但我需要双重身份验证,一个是经过身份验证的用户的默认设置,一个是 azure 的默认设置。
我的天蓝色助手类看起来像:
public static async Task<List<string>> GetAzureVmSizeList(string clientId, string clientSecret, string tenantId, string subscriptionId, string location)
var instanceIds = new List<string>();
var vmSizes = await VirtualMachineSizes(clientId, clientSecret, tenantId, subscriptionId, location);
foreach (var vmSize in vmSizes.Where(x => x.NumberOfCores >= 2 && x.MemoryInMB >= 2048))
instanceIds.Add(vmSize.Name);
return instanceIds;
private static async Task<IEnumerable<VirtualMachineSize>> VirtualMachineSizes(string clientId, string clientSecret, string tenantId,
string subscriptionId, string location)
AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
clientId,
clientSecret,
tenantId,
AzureEnvironment.AzureGlobalCloud);
RestClient restClient = RestClient.Configure()
.WithEnvironment(AzureEnvironment.AzureGlobalCloud)
.WithCredentials(credentials)
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Build();
ComputeManagementClient client = new ComputeManagementClient(restClient.Credentials)
SubscriptionId = subscriptionId
;
var vmSizes = await client.VirtualMachineSizes.ListAsync(location);
return vmSizes;
// 如果可能,获取 Azure 令牌我需要在获取 azure vm 大小列表中使用此令牌
public static async Task<string> GetToken(string azureUrl, string clientId, string
clientSecret)
var url = $"https://login.microsoftonline.com/azureUrl/oauth2/v2.0/token";
var credentials = new Dictionary<string, string>
"client_id", clientId,
"client_secret", clientSecret,
"scope", "https://management.azure.com/.default",
"grant_type", "client_credentials"
;
var client = new HttpClient();
var req = new HttpRequestMessage(HttpMethod.Post, url) Content = new
FormUrlEncodedContent(credentials) ;
var res = await client.SendAsync(req);
var result = res.Content.ReadAsStringAsync().Result;
var tokenObject = JObject.Parse(result);
var token = tokenObject["access_token"];
return token.ToString();
// 我正在使用此助手的控制器:
[AllowAnonymous] // it should be [Authorize]
[HttpGet("azurevm/projectName")]
public async Task<IActionResult> GetAzureList(string projectName)
var credentials = await _context.Projects
.Join(_context.CloudCredentials, project => project.CloudCredentialId, cloud
=> cloud.Id,
(project, cloud) => new project, cloud)
.Where(@t => @t.cloud.IsAzure
&& @t.project.Name == projectName).Select(x =>
new
azureLocation = x.cloud.AzureLocation,
azureClientId = x.cloud.AzureClientId,
azureClientSecret = x.cloud.AzureClientSecret,
azureSubscriptionId = x.cloud.AzureSubscriptionId,
azureTenantId = x.cloud.AzureTenantId,
azureUrl = x.cloud.AzureUrl
).FirstOrDefaultAsync();
if (credentials == null)
return BadRequest($"projectName is not Azure based");
var result = await AzureHelper.GetAzureVmSizeList(credentials.azureClientId,
credentials.azureClientSecret,
credentials.azureTenantId,
credentials.azureSubscriptionId,
credentials.azureLocation);
if (result == null)
return BadRequest($"projectName is not Azure based");
return Ok(result);
//我的启动类jwt配置(或许可以扩展,添加多个jwt)
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
options.TokenValidationParameters = new TokenValidationParameters
ValidateIssuerSigningKey = true,
IssuerSigningKey =
new SymmetricSecurityKey(
Encoding.ASCII.GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
ValidateIssuer = false,
ValidateAudience = false
;
);
【问题讨论】:
【参考方案1】:要管理虚拟机,您不应代表用户使用授权。因此,在那之后,您应该为您的用户获得一项全局授权,并为您的应用程序获得一项 Azure 门户授权。我建议您使用 Microsoft.Azure.Management.ResourceManager 包来获取虚拟机。
【讨论】:
其实我已经在使用 **Microsoft.Azure.Management.ResourceManager.Fluent; Microsoft.Azure.Management.ResourceManager.Fluent.Authentication; Microsoft.Azure.Management.ResourceManager.Fluent.Core; ** 所以不要使用 JWT 令牌。准备这样的凭据: var credentials = new AzureCredentialsFactory() .FromServicePrincipal(servicePrincipleCredentials.ClientId, servicePrincipleCredentials.ClientSecret, servicePrincipleCredentials.TenantId, AzureEnvironment.AzureGlobalCloud);然后将其传递给身份验证方法 Azure.Configure().Authenticate(credentials) Azure 的凭据可以存储在应用程序设置中。我建议你在 Azure 应用服务中使用 Key Vault 或应用程序设置。不要直接将机密存储在 appsettings.json 中。仅在 IIS 中发布时,在 ASP.net Core 3.1 WebAPI 中启用 CORS 时出错
】仅在IIS中发布时,在ASP.netCore3.1WebAPI中启用CORS时出错【英文标题】:ErroronEnableCORSinASP.netCore3.1WebAPIinonlywhenpublishedinIIS【发布时间】:2020-09-2103:42:12【问题描述】:我有一个带有aspcore3.1的api当我使用本地主机时,一切都很好,... 查看详情
在 ASP.NET Core 3.1 中处理多环境的 CORS 策略
】在ASP.NETCore3.1中处理多环境的CORS策略【英文标题】:HandlingCORSpolicyformultipleenvironmentinASP.NETCore3.1【发布时间】:2020-07-1622:08:18【问题描述】:我正在尝试根据应用程序运行的环境在我的CORS策略之间“切换”。我有两个政策声明... 查看详情
如何在 ASP.NET Core 中启用 CORS
】如何在ASP.NETCore中启用CORS【英文标题】:HowtoenableCORSinASP.NETCore【发布时间】:2015-11-0316:39:57【问题描述】:我正在尝试在我的ASP.NETCoreWebAPI上启用跨源资源共享,但我卡住了。EnableCors属性接受policyName类型的string作为参数://Su... 查看详情
如何在 ASP.NET Core 3.1 中使用 Java?
】如何在ASP.NETCore3.1中使用Java?【英文标题】:HowtouseJavainASP.NETCore3.1?【发布时间】:2022-01-0917:41:56【问题描述】:我正在使用ASP.NET构建一个RESTAPI,理想情况下它应该能够使用Java代码。我已经设法在一个简单的控制台应用程序... 查看详情
如何在 ASP.net Core WebAPI 中启用 CORS
】如何在ASP.netCoreWebAPI中启用CORS【英文标题】:HowtoenableCORSinASP.netCoreWebAPI【发布时间】:2021-12-3009:59:00【问题描述】:我要做什么我有一个托管在Azure免费计划上的后端ASP.NetCoreWebAPI(源代码:https://github.com/killerrin/Portfolio-Backend... 查看详情
如何在 Asp.Net Core 3.0 WebAPI 中启用 CORS
】如何在Asp.NetCore3.0WebAPI中启用CORS【英文标题】:HowtoenableCORSinAsp.NetCore3.0WebAPI【发布时间】:2020-09-0721:49:39【问题描述】:我想通过Asp.NetCore3.0API项目启用CORS。这是生成的基本Asp.NetCoreApi模板。模板中的所有内容都是默认设置,... 查看详情
如何在asp .net core 3.1中设置请求超时
】如何在asp.netcore3.1中设置请求超时【英文标题】:howsettherequesttimeoutinasp.netcore3.1【发布时间】:2020-06-1006:04:15【问题描述】:从VisualStudio中选择创建一个新项目。选择ASP.NETCore3.1在IIS中发布和托管增加上传文件大小这段代码:pu... 查看详情
如何在 ASP.NET CORE 3.1 中获取客户端 IP 地址?
】如何在ASP.NETCORE3.1中获取客户端IP地址?【英文标题】:HowdoIgetclientIPaddressinASP.NETCORE3.1?【发布时间】:2020-04-2921:06:39【问题描述】:在WebAPI项目中编写代码时如何在ASP.NET3.1中获取客户端IP地址【问题讨论】:这能回答你的问题... 查看详情
如何在 ASP.NET Core 3.1 生产中配置 IdentityServer
】如何在ASP.NETCore3.1生产中配置IdentityServer【英文标题】:HowtoconfigureIdentityServerinASP.NETCore3.1production【发布时间】:2021-10-2711:31:05【问题描述】:我正在尝试运行ASP.NETCore3.1应用程序,该应用程序在开发中运行良好,但在生产环境... 查看详情
如何在我的 asp.net core 3.1 应用程序中播种用户和角色?
】如何在我的asp.netcore3.1应用程序中播种用户和角色?【英文标题】:HowcanIseedusersandrolesinmyasp.netcore3.1application?【发布时间】:2020-04-0109:17:30【问题描述】:我尝试在我的IdentityDataContext类中使用protectedoverridevoidOnModelCreating(ModelBui... 查看详情
如何在 Asp.net Core 3.1 中使用 Ajax 刷新或重新加载 ViewComponent?
】如何在Asp.netCore3.1中使用Ajax刷新或重新加载ViewComponent?【英文标题】:HowtorefreshorreloadaViewComponentwithAjaxinAsp.netCore3.1?【发布时间】:2021-10-2702:12:33【问题描述】:我希望FavoriteComponent在单击“a”标签时刷新?HTML:<divid="favari... 查看详情
如何在我的 ASP.NET Core Web API 中启用 BSON 序列化?
】如何在我的ASP.NETCoreWebAPI中启用BSON序列化?【英文标题】:HowdoIenableBSONserializationinmyASP.NETCoreWebAPI?【发布时间】:2017-08-3010:00:54【问题描述】:我是ASP.NETCore和一般网络编程的新手。我刚刚成功完成了我的第一个基于RESTfull设... 查看详情
如何使用 Visual Studio 2019 在 Docker 容器中运行 ASP.NET Core 3.1 项目?
】如何使用VisualStudio2019在Docker容器中运行ASP.NETCore3.1项目?【英文标题】:HowtorunanASP.NETCore3.1projectinaDockercontainerwithVisualStudio2019?【发布时间】:2020-04-2114:35:36【问题描述】:我创建了一个基于ASP.NETCore3.1框架的WebAPI项目。后来,... 查看详情
如何使用 C# 在 ASP.NET Core 3.1 MVC 中使用会话变量
】如何使用C#在ASP.NETCore3.1MVC中使用会话变量【英文标题】:HowtousesessionvariablesinASP.NETCore3.1MVCusingC#【发布时间】:2021-09-2605:01:19【问题描述】:我在处理会话对象时遇到了问题。设置和进入同一个控制器可以正常工作,我的麻烦... 查看详情
在 cookie 中存储 JWT 令牌后,如何在 ASP.NET Core 3.1 中破坏该 cookie 并获取信息
】在cookie中存储JWT令牌后,如何在ASP.NETCore3.1中破坏该cookie并获取信息【英文标题】:AfterstoreJWTtokenincookiehowtobreakthatcookieandgetinformationinASP.NETCore3.1【发布时间】:2021-02-2011:28:32【问题描述】:在我的ASP.NETCore3.1MVC应用程序中,我... 查看详情
如何在 ASP.NET Core 3.1 中逐行迭代列表
】如何在ASP.NETCore3.1中逐行迭代列表【英文标题】:HowtoiteratealistrowbyrowinASP.NETCore3.1【发布时间】:2021-03-0208:48:25【问题描述】:我有一个list<>,我需要将列表的每一行传递给一个存储过程。我目前有一个foreach循环来... 查看详情
登录 ASP.NET Core 3.1 后如何获取用户信息
】登录ASP.NETCore3.1后如何获取用户信息【英文标题】:HowtogetUserInformationafterlogininASP.NETCore3.1【发布时间】:2021-10-1908:46:54【问题描述】:我正在尝试在登录ASP.NETCore3.1后获取用户信息(姓名、电子邮件、ID等信息...)。这是我在... 查看详情
如何在 asp.net core 3.1 中将 Json.NET 设置为默认序列化程序
】如何在asp.netcore3.1中将Json.NET设置为默认序列化程序【英文标题】:HowdoIsetJson.NETasdefaultserializerinasp.netcore3.1【发布时间】:2020-04-0102:37:44【问题描述】:将ASP.NET核心2.1Web应用程序转换为3.1并发现一些端点损坏,因为新的Json序... 查看详情