第十三节 身份认证

  1. 添加SettingController,如下图所示:

第十三节 身份认证

  1. Nop.Services.Configuration中的ISettingService.cs、SettingService.cs文件中添加InsertSetting(Setting setting, bool clearCache = true)方法的声明和定义。
  2. 添加LoginModel,如下图所示:

第十三节 身份认证

  1. 在Nop.Web项目中创建Setting文件夹中的Create.cshtml页面。

5按F5在手动生成MyNopCommerce数据库时,导航到Setting/Create页面,同时在页面提交数据时发现在数据库相应的表中出现相应的数据。

        6、重构Home文件夹中的Index.cshtml页面,使用ViewModel项目中的LoginModel为模型类。

        7、重构Controllers项目中的HomeController类,如图所示:

第十三节 身份认证

  1. 在Nop.Services项目中的Authentication文件夹中添加下列定义:CookieAuthenticationService.cs、IAuthenticationService.cs和NopAuthenticationDefaults.cs文件和类。
  2. 定义Nop.Web.Framework.Infrastructure.DependencyRegistrar类的Register方法中不回下列语句。

builder.RegisterType<CookieAuthenticationService>().As<IAuthenticationService>().InstancePerLifetimeScope();

10、按F5导航到home/index页面,同时提交登录数据时,在下列语句await _httpContextAccessor.HttpContext.SignInAsync(NopAuthenticationDefaults.AuthenticationScheme, userPrincipal, authenticationProperties);会出现异常:“No sign-in authentication handlers are registered. Did you forget to call AddAuthentication().AddCookies(Authentication) “

11、在Nop.Web.Framework.Infrastructure.Extensions.ServiceCollectionExtensions类中添加方法:

public static void AddNopAuthentication(this IServiceCollection services)

12、定义方法类:NopCookieDefaults.cs和IExternalAuthenticationRegistrar.cs如图所示:

第十三节 身份认证

13、在Nop.Web.Framework.Infrastructure.AuthenticationStartup类的ConfigureServices方法中添加语句:

//添加身份验证。

            services.AddNopAuthentication();

14、按F5导航到home/index页面,同时提交登录数据时, 页面会按照要求跳转到Setting/Create页面,同时已经解决第10步所出现的异常。

至于更新详细的信息请见我所上传代码“2019-10-17_MyNopCommerce_VS19Core2.2Nop4.20(不删,010_身份验证)。(https://download.csdn.net/download/zhoujian_911/11873625)