首先 在serveices 方法 注册容器 告诉容器时 cookis 形势
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddAuthentication(option =>
{
option.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
option.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, option=> {
option.LoginPath = "/home/index";
option.AccessDeniedPath = "/home/Privacy";
});
///这里是 授权 运用policy 策略 可以连接自己数据库灵活扩展
services.AddAuthorization(option =>
{
option.AddPolicy("AdminPolicy", policy => {
policy.Requirements.Add(new DoubleEmailRequirement());
});
option.AddPolicy("QQEmail", policy =>
{
policy.Requirements.Add(new DoubleEmailRequirement());
});
});
services.AddSingleton<IAuthorizationHandler, QQHander>();
services.AddSingleton<IAuthorizationHandler, Handler163>();
}
在 Configure 方法类 别忘记 添加鉴权 和授权的 中间件
app.UseAuthentication();
app.UseAuthorization()
在EmailRequirement 中继承IAuthorizationRequirement 标识 至于为什么这么做需要自己解读源码