Asp.net core 使用asp.net core 3.1 web api的解决方案是什么
我正在使用asp.net web api应用程序和owin实现jwt安全性,如下所示Asp.net core 使用asp.net core 3.1 web api的解决方案是什么,asp.net-core,Asp.net Core,我正在使用asp.net web api应用程序和owin实现jwt安全性,如下所示 using Microsoft.Owin; using Owin; using System.Web.Http; using Microsoft.Owin.Security; using Microsoft.Owin.Security.Jwt; [assembly: OwinStartup(typeof(solution.Startup))] namespace solution {
using Microsoft.Owin;
using Owin;
using System.Web.Http;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Jwt;
[assembly: OwinStartup(typeof(solution.Startup))]
namespace solution
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
ConfigureOAuth(app);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
public void ConfigureOAuth(IAppBuilder app)
{
var issuer = "issuer";
var audience = "audience";
var secret = JwtSecurityKey.Create("SecurityKey").GetSymmetricKey();
// Api controllers with an [Authorize] attribute will be validated with JWT
var option =
new JwtBearerAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = new[] { audience },
IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
{
new SymmetricKeyIssuerSecurityTokenProvider(issuer, secret)
}
};
app.UseJwtBearerAuthentication(
option
);
}
}
}
关于如何将其转换为asp.net核心web api应用程序的任何指南或教程?在启动文件中,您可以执行类似的操作: 配置服务方法
// Configure JWT authentication
var key = Encoding.UTF8.GetBytes(AppConfig.Secret);
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidateIssuer = false, //or true
ValidateAudience = false, //or true
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
//ValidIssuer = "somewhere.com",
//ValidAudience = "somewhere.com",
IssuerSigningKey = new SymmetricSecurityKey(key)
};
});
…并在配置方法中
app.UseAuthentication();
您可以查看教程: