我遇到了一种以前在 .NET 中从未见过的错误。我对解决这个问题的方法有点困惑。
我不明白“要定义的声明”错误。具体来说, 对类型“TokenValidationParameters”的引用声称它是在“System.IdentityModel.Tokens.Jwt”中定义的,我使用过 DotNetPeak。 TokenValidationParameters 肯定在 Microsoft.IdentityModel.Tokens 中 而不是在我的 System.IdentityModel.Tokens.Jwt.
副本中那么什么是“索赔”?它怎么知道的?我刚刚完成了将大约 100 个 NuGet 包对齐的过程。这是我的最后一期。我似乎找不到可以在这里工作的代码。我在 .NET 4.8 的 ASP.NET API 5 项目中。
我的代码如下
using MyApiProject.Shared;
using System.IdentityModel.Tokens;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.ActiveDirectory;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;
using Owin;
namespace MyApiProject
{
public class AuthConfig
{
public void ConfigureAuth(IAppBuilder app, IApiSettings appSettings)
{
var tokenValidationParameters = new TokenValidationParameters();
tokenValidationParameters.ValidAudience = appSettings.Get("aad-audience-id");
tokenValidationParameters.RoleClaimType = "roles";
var authOptions = new WindowsAzureActiveDirectoryBearerAuthenticationOptions();
authOptions.Tenant = appSettings.Get("aad-tenant-id");
authOptions.TokenValidationParameters = tokenValidationParameters;
var openIdConnectOptions = new OpenIdConnectAuthenticationOptions
{
ClientId = appSettings.Get("aad-audience-id"),
Authority = appSettings.Get("aad-authority"),
RedirectUri = appSettings.Get("aad-redirect-uri") + "/hangfire"
};
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseWindowsAzureActiveDirectoryBearerAuthentication(authOptions);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(openIdConnectOptions);
}
}
}