gpt4 book ai didi

c# - .NET Core 和 Azure Active Directory 集成

转载 作者:行者123 更新时间:2023-11-30 23:12:13 24 4
gpt4 key购买 nike

我在 Azure Active Directory 中使用 token 身份验证(而不是 cookie)。

基于这篇文章:https://www.itunity.com/article/angular-2-openid-connect-azure-active-directory-3093

我能够让它在客户端工作。

   public validateSignature(token): Observable<boolean> {
/* Retrieve from federated metadata endpoint.
In this sample, the document was downloaded locally */
return this.httpService.get("metadata/metadata.xml")
.map((res: Response) => {
let dom = (new DOMParser()).parseFromString(res.text(), "text/xml");
let json = xml2json(dom, "");
let cert = "-----BEGIN CERTIFICATE-----" +
JSON.parse(json).EntityDescriptor[0]["ds:Signature"]
["KeyInfo"]["X509Data"]["X509Certificate"] +
"-----END CERTIFICATE-----";
let key = KEYUTIL.getKey(cert);
return KJUR.jws.JWS.verifyJWT(token, key, { alg: ['RS256'] });
})
}

我试图在 .NET Core 1.0.3 中重新实现上述方法。

基于这篇文章:how to sign and verify signature with net and a certificate

以下行不会在 .NET Core 上编译:

RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PublicKey.Key;

我不确定在 .NET Core 中基于证书验证 token 的正确方法是什么。

最佳答案

验证 Azure AD 颁发的 token 的一种简单方法是利用带有 Web API 的 OWIN 注释。我们只需要配置 JwtBearerOptions 并将请求发送到受 Azure AD 保护的 Controller 。如果 token 未通过验证,您将收到 401 响应。您可以引用代码示例 here .

如果你想实现手动验证 token 的代码,我们可以引用Microsoft.AspNetCore.Authentication.JwtBearer中微软如何验证 token 的代码。 .

我也写了一个代码示例供大家引用:

public class JsonWebTokenValidator
{
public void Validate(string token)
{
var stsDiscoveryEndpoint = "https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration";
var options = new JwtBearerOptions
{
ConfigurationManager = new ConfigurationManager<OpenIdConnectConfiguration>(stsDiscoveryEndpoint, new OpenIdConnectConfigurationRetriever()),

TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
{
ValidateIssuer = true,
ValidIssuer = "https://sts.windows.net/{tenantId}/",

ValidateAudience = true,
ValidAudience = "{audience}",

RequireExpirationTime = true,
ValidateLifetime = true,

ValidateIssuerSigningKey = true,

ClockSkew = TimeSpan.Zero
},
Authority = "https://login.microsoftonline.com/{tenantId}",
};

SecurityToken validatedToken = null;
ClaimsPrincipal result = null;
var configuration = options.ConfigurationManager.GetConfigurationAsync(new CancellationToken()).Result;
options.TokenValidationParameters.IssuerSigningKeys = configuration.SigningKeys;

options.ConfigurationManager.RequestRefresh();
foreach (var validators in options.SecurityTokenValidators)
{
result = validators.ValidateToken(token, options.TokenValidationParameters, out validatedToken);
}

foreach (var claim in result.Claims)
{
Console.WriteLine($"{claim.Subject}:{claim.Value}");
}
}

项目.json

{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},

"dependencies": {
"Microsoft.IdentityModel.Clients.ActiveDirectory": "3.13.9",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
},

"System.IdentityModel.Tokens.Jwt": {
"version": "5.1.3"
},
"Microsoft.AspNetCore.Authentication.JwtBearer": "1.0.0",
"Microsoft.IdentityModel.Protocols": "2.1.3",
"Microsoft.IdentityModel.Protocols.OpenIdConnect": "2.0.0"
},

"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}

关于c# - .NET Core 和 Azure Active Directory 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44272287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com