我有一个在.net core 3.1上开发的Web应用程序,我使用CookiAuthentication和Jwt身份验证Japi用于我的api,并且cookiauthentication用于表单 它在本地工作正常,但是当我在服务器上发布站点时,jwt身份验证不起作用 我的网站托管在Azure上,我不知道这可能会导致此问题 我的启动代码:
public void ConfigureServices(IServiceCollection services)
{
var connection = Configuration.GetConnectionString("LocalDB");
services.AddDbContext<Models.CoreDBContext>(options => options.UseSqlServer(connection));
services.Configure<MySettings>(Configuration.GetSection("MySettings"));
services.AddControllersWithViews().AddNewtonsoftJson();
services.AddMvc();
services.AddMvcCore().AddAuthorization();
services.AddRazorPages().AddRazorRuntimeCompilation();
services.AddIdentity<Models.AspNetUser, IdentityRole>()
.AddEntityFrameworkStores<Models.CoreDBContext>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options =>
{
// Password settings.
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 1;
// Lockout settings.
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;
// AspNetUser settings.
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = false;
});
services.ConfigureApplicationCookie(options =>
{
options.AccessDeniedPath = "/Account/AccessDenied";
options.Cookie.Name = "YourAppCookieName";
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
options.LoginPath = "/Account/Login";
options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
options.SlidingExpiration = true;
});
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(20);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
services.AddMvc().AddNewtonsoftJson(opt =>
{
opt.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
services
.AddAuthentication(
)
.AddCookie(cng =>
{
cng.LoginPath = "/User/Login";
cng.AccessDeniedPath = "/User/AccessDenied";
})
.AddJwtBearer(options =>
{
options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuerSigningKey = true,
ValidateLifetime = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Enums.JwtBarearKey)),
};
});
}