asp.net-core - 当我将身份添加到项目时,为什么我的 OAuth 提供程序无法正常工作?
问题描述
我有使用 OAuth 对用户进行身份验证的代码。这是这段代码:Github 链接
我ConfigureServices()
在 Startup 类的方法中使用了这段代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options=>
{
options.LoginPath = new PathString("/Account/Login");
options.LogoutPath = new PathString("/Account/Logout");
options.AccessDeniedPath = new PathString("/Account/Forbidden");
})
.AddVkontakte(options => // here
{
options.ApiVersion = "5.95";
options.ClientId = Configuration["VKontakte:ClientId"];
options.ClientSecret = Configuration["VKontakte:ClientSecret"];
});
services.AddDefaultIdentity<User>(options =>
{
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
})
.AddEntityFrameworkStores<ApplicationContext>()
.AddDefaultTokenProviders();
services.AddMvc();
}
但是当我尝试使用它进行身份验证时,什么也没有发生。它以我想要的方式工作,只有当我删除这个笔画时
...
services.AddDefaultIdentity<User>(options =>
{
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
})
.AddEntityFrameworkStores<ApplicationContext>()
.AddDefaultTokenProviders();
在这两种情况下,后面的代码都.AddVkontakte(...)
可以正常工作,我在浏览器的网络检查器中检查了它。我的代码向 OAuth 提供者 (vk.com) 发出请求并成功获得响应。但我不明白为什么AddDefaultIdentity<User>(...)
不允许.AddVkontakte(...)
对用户进行身份验证。
你怎么看待这件事?
解决方案
好的,我查看了这个问题(Asp Core 2.1 Jwt + Identity.userManager store does not implement IUserRoleStore),并尝试更改传递给的一些选项AddAuthentication
,并且成功了!
这是最终代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options=> // defined some options
{
options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ApplicationScheme;
})
.AddCookie(options=>
{
options.LoginPath = new PathString("/Account/Login");
options.LogoutPath = new PathString("/Account/Logout");
options.AccessDeniedPath = new PathString("/Account/Forbidden");
})
.AddVkontakte(options =>
{
options.ApiVersion = "5.95";
options.ClientId = Configuration["VKontakte:ClientId"];
options.ClientSecret = Configuration["VKontakte:ClientSecret"];
});
services.AddDefaultIdentity<User>(options =>
{
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
})
.AddEntityFrameworkStores<ApplicationContext>()
.AddDefaultTokenProviders();
services.AddMvc();
}
我不知道这是什么意思,但它有效!等着瞧。
推荐阅读
- javascript - Moment.js 仅在第 12 天之前过滤
- javascript - 使用 find() 在 JSON 中查找对象
- javascript - 如何找到每个句子的最后一个词
- python-3.x - ImportError:无法在 MacOS Catalina 上导入名称“NoReturn”PyTorch
- c++ - C++17 中的新表达式求值顺序
- python - llvmlite 安装失败。构建 llvmlite 时出错
- python - 删除 bs4 中请求的 href attrs 的某些部分
- python - 使用 Python 将 SQL 输出读入 SAS 数据集时出现问题
- kotlin - 整个包的quarkus本机反射配置
- amazon-web-services - AWS Instance Scheduler 中的计划未按计划工作