为什么我的OAuth提供商在项目中添加身份时会错误地工作



我的代码可以使用OAuth来验证用户。这是此代码:github链接
我在启动类的ConfigureServices()方法中使用此代码:

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 身份。USERMANAGERStore不实现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();
}

我不知道这是什么意思,但它有效!等待。

最新更新