我希望重用我当前定义的Controller/Action Authorize属性来指定用户访问角色以显示菜单项(因此用户只能看到他们有权访问的菜单项).
目前,菜单项的显示和控制器/动作授权属性角色是不同的,因此任何更改都需要在两个地方进行更新,这可能在将来容易出错.
我一直在研究自定义授权属性,这是我到目前为止所拥有的:
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var isAuthorized = base.AuthorizeCore(httpContext);
if (!isAuthorized)
{
return false;
}
var routeData = httpContext.Request.RequestContext.RouteData;
string currentAction = routeData.GetRequiredString("action");
string currentController = routeData.GetRequiredString("controller");
var currentUserRoles = GetCurrentUserRoles();
// from the list of menu items, find the menu item with the current action
// and controller and check the current user's roles entitle access
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.Result = new RedirectToRouteResult(
new …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用C#和RabbitMQ在MassTransit v3中实现仅发布总线,其中总线没有消费者.概念是消息将被发布和排队,然后单独的微服务将消耗来自队列的消息.查看此SO答案,必须指定接收端点,以便实际排队邮件.然而,这似乎与MassTransit文档中常见的问题相矛盾,后者表示If you need to only send or publish messages, don’t create any receive endpoints
.
以下是一些示例代码:
public class Program
{
static void Main(string[] args)
{
var bus = BusConfigurator.ConfigureBus();
bus.Start();
bus.Publish<IItemToQueue>(new ItemToQueue { Text = "Hello World" }).Wait();
Console.ReadKey();
bus.Stop();
}
}
public static class BusConfigurator
{
public static IBusControl ConfigureBus()
{
var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var host = cfg.Host(new Uri("rabbitmq://localhost/"), hst =>
{
hst.Username("guest");
hst.Password("guest");
});
cfg.ReceiveEndpoint(host, "queuename", e =>
{ …
Run Code Online (Sandbox Code Playgroud)