NopCommerce 3. Controller 分析

1. 继承关系,3个abstract类

System.Web.Mvc.Controller

                Nop.Web.Framework.Controllers.BaseController

                              Nop.Admin.Controllers.BaseAdminController

2. BaseController

1. RenderPartialViewToString()

          将Viewname对应的页面加载成string

a. IViewEngine.FindPartialView() 来获取ViewEngineReult,够着viewcontext 实例,作为Render()的参数

b.IView.Render() 来加载到streamwriter中

c. streamwriter.Tostring()返回数据


可以扩展下压缩html的方法

html=Regex.Replace(html,”\n+\s+”,string.empty);

html=Regex.Replace(html,”\n+”,string.empty);

html=html.trim();


2. 各种发送消息的方法,主要是配合View来显示后台的消息

是利用ViewData,TempData来传递数据,然后来View中读取循环显示


3. AddLocales()

3.  BaseAdminController

1. 无权限访问返回错误的方法

a. ActionResult AccessDeniedView();

b. JsonResult AccessDeniedKendoGridJson();


4. StoreAddressAttribute

做各种判断,如果不符合则return

只记录Get请求的Ip地址,更新Customer表中LastIpAddress字段

5. CustomerLastActivityAttribute

记录最后登录的时间

更新最后活动时,1分钟内数据不更新,防止频繁修改,更新Customer表中LastActivityDateUtc字段

6. StoreLastVisitedPageAttribute

记录最后查看的页面

保存在GenericAttribute表中,需要单独启动是否记录

7. ValidatePasswordAttribute

检验用户是不是需要定时修改密码,默认过期时间在setting表中的PasswordLifetime 字段中设置,默认是90天

如果未设置密码的用户,则下次请求时立即要求修改密码


修改密码就是强制调整到/CustomerChangePassword页面中

8. NopHttpsRequirementAttribute

对get请求判断强制跳转ssl的地址,在setting表中ForceSslForAllPages中设置是否启用对所有页面强制跳转ssl

先判断当前请求是否是ssl,再查看当前的店是否支持ssl,如果支持的话,则跳转到ssl地址


如果setting中未启用该选项,则使用301跳转到当前页面,这不是多此一举吗?301 应该有玄机.

9. AdminValidateIpAddress

限制Admin后台网页不被其他ip访问,保护后台安全

在setting表中的AdminAreaAllowedIpAddresses属性中设置允许访问的ip地址,如果AdminAreaAllowedIpAddresses不为空,此时判断当前request的ip地址是否在其中,不在其中则跳转到/admin/security/accessdenied

10. AdminAuthorizeAttribute:IAuthorizationFilter

继承自IAuthorizationFilter 接口,需要实现void OnAuthorization(AuthorizationContext filterContext);


可以在构造函数中指定bool 来跳过验证

核心方法OnAuthorization方法中,首先在IsAdminPageRequested()方法中判断Controller或Action上是否定义了AdminAuthorize属性,


如果没有权限访问该页面,则返回401 HandleUnauthorizedRequest


权限是定义在PermissionRecord 中,根据sysname 和roleId 来判断权限

首先是每个customer和role有一个多对多的关联

然后是每个role和permissionrecord有一个多对多的关联


验证权限就是遍历该用户的每一个role,然后判断这个role对应的permissionrecord集合中是否包含当前这个页面访问所需要的权限


相关的表有


  1. Customer-客户信息表
  2. CustomerRole-客户角色表
  3. Customer_CustomerRole_Mapping-用户角色表映射
  4. PermissionRecord-权限记录表
  5. PermissionRecord_Role_Mapping-角色权限记录关系表


11. AdminAntiForgeryAttribute:IAuthorizationFilter

是对ValidateAntiForgeryTokenAttribute属性的封装,但是新增了配置的功能


可以在setting表中的EnableXsrfProtectionForAdminArea中配置

这个封装的不错,有灵活性,还能有效利用系统原有代码

12. AdminVendorValidation:IAuthorizationFilter

阻止非商户角色的用户登录后台

五种角色,每种角色对应不同的permissionrecord,就可以决定哪些页面可以访问了

  1. 管理员
  2. 论坛版主
  3. 注册用户
  4. 访客
  5. 商户


这就是
原文地址:https://www.cnblogs.com/zhangrCsharp/p/7681997.html