本节将通过对“2019-10-17_MyNopCommerce_VS19Core2.2Nop4.20(不删,010_身份验证)”程序进行重构,从而来实现授权功能。
- 通过对nopCommerce应用程中的网页的参看,发现许多网页的跳转操作都是通过@Url.RouteUrl实现的,但是“2019-10-17_MyNopCommerce_VS19Core2.2Nop4.20(不删,010_身份验证)”程序,并不支持@Url.RouteUrl操作。所以需要“2019-10-17_MyNopCommerce_VS19Core2.2Nop4.20(不删,010_身份验证)”程序结合“2019-10-28_MyNopCommerce_VS19Core2.2Nop4.20(不删011_配置管道请求)结自身进行重构,使其自身支持@Url.RouteUrl操作。
- 同时许多网页的跳转操作也需要WebHelper类所定义的操作,所有下面将要完成WebHelper类的定义。
- 添加如图中所选择类的定义:
4、重构Nop.Web.Framework.Infrastructure.DependencyRegistrar类
在 public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)方法中,添加下列语句:
//WEB助手
builder.RegisterType<WebHelper>().As<IWebHelper>().InstancePerLifetimeScope();
- 设置端点到网页到Index、Login,并按F5可以发现WebHelper类的实例已经可以获取数据。
- 添加如图中所选择类的定义:
- 重构Nop.Services.Installation.CodeFirstInstallationService类
(1)、添加InstallStores()方法的定义。
(2)、在public virtual void InstallRequiredData(string defaultUserEmail, string defaultUserPassword)方法中添加下列语句:
InstallStores();
- 添加IStoreService和StoreService定义。
8、重构Nop.Web.Framework.Infrastructure.DependencyRegistrar类
在 public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)方法中,添加下列语句:builder.RegisterType<StoreService>().As<IStoreService>().InstancePerLifetimeScope();
9、并按F5可以发现数据库中的Store表中的数据已经被初始化,同时为了减少代码但注释了InstallStores()方法中的_webHelper语句。
10、对HomeController.cs和SettingController.cs文件中的方法进行重构。
11、重构IPermissionService和PermissionService在其中添加Authorize方法的声明和定义。
12、添加IWorkContext.cs和WebWorkContext.cs定义。
13、重构Nop.Web.Framework.Infrastructure.DependencyRegistrar类
在 public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)方法中,添加下列语句:
//工作上下文
builder.RegisterType<WebWorkContext>().As<IWorkContext>().InstancePerLifetimeScope();
14、添加Nop.Services.Tasks.NopTaskDefaults定义,并重构ICustomerService和CustomerService添加相关方法的声明和定义。
15、添加Nop.Core.Domain.Customers.CustomerExtensions定义,并重构IAuthenticationService和CookieAuthenticationService添加相关方法的声明和定义。
16、并按F5可以发现Nop.Services.Authentication.CookieAuthenticationService类中的GetAuthenticatedCustomer()方法可以获取customer实例值,并不为null,而_cachedCustomer为null。但是Nop.Web.Framework.WebWorkContext类的CurrentCustomer属性中的customer = _authenticationService.GetAuthenticatedCustomer();语句中的customer实例值,则为null。而在nopCommerc应用程序中如果上述语句中的customer实例值,为null,则会通过不断的迭代的查找使上述语句中的customer实例值,不为null。但是在自己开发的程序中则不能实现迭代查找功能,所以上述语句中的customer实例值,仍为null。
17、重构HomeController类中的public IActionResult Login(LoginModel model, string returnUrl)方法为:
//return new RedirectToActionResult("Create", "Setting", null);
if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers))
return View(model);
return View(model);
18、重构ICustomerService和CustomerService添加GetCustomerByGuid方法的声明和定义。
19、重构Nop.Web.Framework.WebWorkContext类在CurrentCustomer属性中添加以下代码:
if (customer == null || customer.Deleted || !customer.Active || customer.RequireReLogin)
{
//获取客户
var customerCookie = GetCustomerCookie();
if (!string.IsNullOrEmpty(customerCookie))
{
if (Guid.TryParse(customerCookie, out Guid customerGuid))
{
// 从cookie获取客户(不应注册)
var customerByCookie = _customerService.GetCustomerByGuid(customerGuid);
if (customerByCookie != null && !customerByCookie.IsRegistered())
customer = customerByCookie;
}
}
}
20、添加断点到 if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers))、
if (customer == null || customer.Deleted || !customer.Active || customer.RequireReLogin)
{
//尝试获取注册用户。
customer = _authenticationService.GetAuthenticatedCustomer();
}和
//缓存已验证的客户。
_cachedCustomer = customer;
return _cachedCustomer;
}
并按F5可以发现Nop.Services.Authentication.CookieAuthenticationService类中的GetAuthenticatedCustomer()方法可以获取customer和_cachedCustomer实例值,都不为null。同时Nop.Web.Framework.WebWorkContext类的CurrentCustomer属性中的customer = _authenticationService.GetAuthenticatedCustomer();语句中的customer实例值也能获取一个具体的不为的null实体值,到此已经解决了16步出现的问题。
- 新详细的信息请见我所上传代码“2019-10-30_MyNopCommerce_VS19Core2.2Nop4.20(不删012_授权).rar。(https://download.csdn.net/download/zhoujian_911/11953169)”。