首页 > 解决方案 > 当我将身份添加到项目时,为什么我的 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.net-coreasp.net-core-mvc

解决方案


好的,我查看了这个问题(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();
}

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


推荐阅读