获取客户角色 NopCommerce
本文关键字:NopCommerce 角色 客户 获取 | 更新日期: 2023-09-27 17:56:38
谁能帮我在 2.8 版的视图上获取客户角色?我尝试过工作上下文,但错误说"当前上下文中不存在工作上下文"。我在下面有以下代码,但它不起作用。
@model BoardsIndexModel
@using Nop.Core.Infrastructure;
@using Nop.Services.Helpers;
@using Nop.Web.Models.Boards;
@using Nop.Core.Domain.Customers;
@using Nop.Services.Customers;
@if (Roles.IsUserInRole("Administrators"))
{
Do that;
}
else
{
Do this;
}
这就是我的做法,在nopcommerce论坛上的一个好人的帮助下:
@using Nop.Core.Domain.Customers;
@using Nop.Services.Customers;
@using Nop.Core;
@{
bool customerHasRoleX = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("Administrators");
bool customerHasRoleY = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("ForumModerators");
}
@if ((customerHasRoleX == true) | (customerHasRoleY == true)) {
}
else
{
Response.Redirect("~/login?ReturnUrl=%2fboards");
}
您可以使用,
@User.IsInRole("Administrators")