小编Ric*_*use的帖子

重用MVC控制器角色授权以显示菜单项

我希望重用我当前定义的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# asp.net-mvc-3

6
推荐指数
1
解决办法
2278
查看次数

使用C#和RabbitMQ在MassTransit v3中实现仅发布总线

我正在尝试使用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)

c# masstransit rabbitmq

6
推荐指数
1
解决办法
1504
查看次数

标签 统计

c# ×2

asp.net-mvc-3 ×1

masstransit ×1

rabbitmq ×1