如何在没有实体框架提供程序的情况下在.net核心中实现google登录 [英] how to implement google login in .net core without an entityframework provider

查看:98
本文介绍了如何在没有实体框架提供程序的情况下在.net核心中实现google登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为.net核心网站实施Google登录.

I am implementing Google login for my .net core site.

在此代码中

var properties = signInManager.ConfigureExternalAuthenticationProperties("Google", redirectUrl);      
return new ChallengeResult("Google", properties); 

我需要一个signInManager,它是(通过代码示例):

I need a signInManager which is (by the code example) this:

private SignInManager<AppUser> signInManager;

我通过构造函数将其注入,然后出现此错误:

I inject it via the constructor, and then I get this error:

尝试激活"AccountController"时,无法解析类型为"Microsoft.AspNetCore.Identity.SignInManager1 [AppUser]"的服务.

Unable to resolve service for type 'Microsoft.AspNetCore.Identity.SignInManager1[AppUser]' while attempting to activate 'AccountController'.

谷歌搜索得知我应该包括这个

Googling learnt that I should include this

services.AddIdentity<AppUser, IdentityRole>()
    .AddDefaultTokenProviders();`

但这给了我这个错误:

尝试激活"Microsoft.AspNetCore.Identity.IUserStore1 [AppUser]"时无法解析类型为"Microsoft.AspNetCore.Identity.IUserStore1 [AppUser]"的服务.

Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore1[AppUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.AspNetUserManager1[AppUser]'.

在那一刻,我得到建议添加此内容:

And at that moment, I get the advice to add this:

.AddEntityFrameworkStores<ApplicationDbContext>()

但是我迷路了,因为为什么SignInManager需要一个IUserStore,我应该添加一个 UserStoreDBContextEntityFramework商店,什么时候不使用(用于Google登录)?

But then I'm lost, because why does the SignInManager need a IUserStore, and should I add a UserStore and a DBContext and an EntityFramework store, when I will not be using that (for my Google login)?

所以问题是:我也可以在没有Entityframework存储的情况下进行Google登录吗?

So the question is: can I also do my Google login without the Entityframework store?

推荐答案

如果您要做的只是使用Google登录,则不需要SignInManagerUserManager或ASP.NET Core Identity本身.为此,我们首先需要配置身份验证服务.这是与此相关的代码,我将在后面解释:

If all you want to do is sign-in with Google, there's no need for SignInManager, UserManager or ASP.NET Core Identity itself. To achieve this, we first need to configure the Authentication services. Here's the relevant code for this, which I'll explain after:

Startup.cs

services
    .AddAuthentication(o =>
    {
        o.DefaultScheme = "Application";
        o.DefaultSignInScheme = "External";
    })
    .AddCookie("Application")
    .AddCookie("External")
    .AddGoogle(o =>
    {
        o.ClientId = ...;
        o.ClientSecret = ...;
    });

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆