第十五节 授权

        本节将通过对“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步出现的问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值