【发布时间】:2018-09-15 12:54:22
【问题描述】:
我在 ASP.NET Core 2.1 中有一个有效的 Google 身份验证流程。
我想在用户登录时根据数据库检查用户的电子邮件地址来添加一些授权。如何在此处访问实体框架DbContext?
public void ConfigureServices(IServiceCollection services)
{
services
.AddDbContext<Database>(options => options.UseSqlServer(Configuration["SqlServer"]));
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(o =>
{
o.LoginPath = "/Auth/SignIn";
o.LogoutPath = "/Auth/SignOut";
o.ExpireTimeSpan = TimeSpan.FromDays(90);
})
.AddGoogle(o =>
{
o.ClientId = Configuration["Auth:Google:ClientId"];
o.ClientSecret = Configuration["Auth:Google:ClientSecret"];
o.Events = new OAuthEvents()
{
OnTicketReceived = async context =>
{
var email = context.Principal.Identity.GetEmail().ToLowerInvariant();
if (/* User not in database */) // <-- How can I access the EF DbContext here?
{
context.Response.Redirect("/Auth/Unauthorised");
context.HandleResponse();
}
}
};
});
services.AddMvc();
/* ... */
}
【问题讨论】:
-
这不是最干净的,也可能不是最安全的解决方案,因此您可以将其作为备用计划。我曾经在它响应的控制器操作中执行此操作
标签: asp.net-core dependency-injection authorization entity-framework-core asp.net-core-identity