我知道在线上有成千上万的教程或讲解,但是我不知道代码中的问题在哪里。 我首先不想设置Cookie身份验证。这是我在Startup.cs中编写的方式:
services.AddDefaultIdentity<ApplicationUser>(options => {
options.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<IdentityDbContext<ApplicationUser>>()
.AddDefaultTokenProviders();
services.AddDbContext<IdentityDbContext<ApplicationUser>>(
options => options.UseSqlServer(string.Format(Configuration["Core:Database:ConnectionString"], "myCoin_Auth")));
services.AddAuthentication()
.AddCookie(cfg =>
{
cfg.AccessDeniedPath = "/plugin/user/AccessDenied";
cfg.LoginPath = "/plugin/user/login";
cfg.LogoutPath = "/plugin/user/login";
cfg.Cookie.IsEssential = true;
})
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Dashboard}/{action=Index}/{id?}");
});
在这里,我验证输入:
[AllowAnonymous]
[Route("login")]
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
_logger.LogInformation("User logged in.");
if (Url.IsLocalUrl(returnUrl))
return Redirect(returnUrl);
return RedirectToAction("Index", "Dashboard");
}
}
...
我的IdentityDbContext和ApplicationUser:
public class IdentityDbContext<TUser>
: IdentityUserContext<TUser, string>
where TUser : IdentityUser
{
public IdentityDbContext(DbContextOptions<IdentityDbContext<ApplicationUser>> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
[Table("AspNetUsers")]
public class ApplicationUser : IdentityUser
{
}
到目前为止我尝试过的:
当我现在说使用[Authorize(Scheme = cookie)]属性重定向到操作时,它自动返回到登录操作。当我删除[Authorized]属性后,它就会起作用,并且会加载网站。
我在做什么错? 预先感谢。