首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Elmah.Mvc 2.0自定义身份验证

Elmah.Mvc 2.0自定义身份验证
EN

Stack Overflow用户
提问于 2013-08-20 12:36:47
回答 2查看 1.8K关注 0票数 5

我正在尝试为Elmah.Mvc 2.0实现自定义身份验证。我知道我的web.config中有两个键(elmah.mvc.allowedRoles和elmah.mvc.allowedUsers),但这对我来说是不够的。

我有一个自定义的表单身份验证方法,它在cookie中添加了一些随机的盐分,所以我没有明确的用户名来表示elmah.mvc.allowedUsers值。而且,我没有实现任何角色。

是否有任何方法覆盖ElmahController或某些Elmah身份验证类/方法?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-23 11:26:46

关于这个问题正在进行的讨论在哪里- https://github.com/alexanderbeletsky/elmah-mvc/pull/24

目前,这是不可能的,但在票证中您可以看到几种解决方案,包括自定义过滤器。我仍然不确定是否需要在Elmah.MVC包中完成一些特殊的工作。

票数 4
EN

Stack Overflow用户

发布于 2013-08-20 12:46:21

您完全可以这样做,并重写ElmahController。事实上,贝列茨基已经提供了一个名为Elmah.Mvcnuget包装

一旦创建了自己的ElmahController,就可以对其应用任何授权。在我的应用程序中,我有一个基本的授权控制器来应用它。您只需要配置您的路由并返回Elmah结果,所有这些都会在他的站点上得到很好的记录。

更新:我已经有一段时间没有看过这个了,但是我有自己的控制器,受到上面的启发,但实际上并没有使用它。

代码语言:javascript
运行
复制
[Authorize]
public class ElmahController : BaseAuthorizedController
{
    public ActionResult Index(string type)
    {
        return new ElmahResult(type);
    }
}

结果是

代码语言:javascript
运行
复制
using System;
using System.Web;
using System.Web.Mvc;

namespace Epic.Mvc.Mvc.ActionResults
{
    public class ElmahResult : ActionResult
    {
        private readonly string _resouceType;

        public ElmahResult(string resouceType)
        {
            _resouceType = resouceType;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            var factory = new Elmah.ErrorLogPageFactory();

            if (!string.IsNullOrEmpty(_resouceType))
            {
                var pathInfo = "/" + _resouceType;
                context.HttpContext.RewritePath(FilePath(context), pathInfo, context.HttpContext.Request.QueryString.ToString());
            }

            var currentApplication = (HttpApplication)context.HttpContext.GetService(typeof(HttpApplication));
            if (currentApplication == null) return;
            var currentContext = currentApplication.Context;

            var httpHandler = factory.GetHandler(currentContext, null, null, null);
            if (httpHandler is IHttpAsyncHandler)
            {
                var asyncHttpHandler = (IHttpAsyncHandler)httpHandler;
                asyncHttpHandler.BeginProcessRequest(currentContext, r => { }, null);
            }
            else
            {
                httpHandler.ProcessRequest(currentContext);
            }
        }

        private string FilePath(ControllerContext context)
        {
            return _resouceType != "stylesheet" ? context.HttpContext.Request.Path.Replace(String.Format("/{0}", _resouceType), string.Empty) : context.HttpContext.Request.Path;
        }
    }
}

我有两条路(第二条非常可选)

代码语言:javascript
运行
复制
routes.MapRoute("ElmahHandler", "elmah/{type}", new { action = "Index", controller = "Elmah", type = UrlParameter.Optional });
            routes.MapRoute("ElmahHandlerShortHand", "errors/{type}", new { action = "Index", controller = "Elmah", type = UrlParameter.Optional });
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18335243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档