我正在尝试为Elmah.Mvc 2.0实现自定义身份验证。我知道我的web.config中有两个键(elmah.mvc.allowedRoles和elmah.mvc.allowedUsers),但这对我来说是不够的。
我有一个自定义的表单身份验证方法,它在cookie中添加了一些随机的盐分,所以我没有明确的用户名来表示elmah.mvc.allowedUsers值。而且,我没有实现任何角色。
是否有任何方法覆盖ElmahController或某些Elmah身份验证类/方法?
谢谢!
发布于 2013-08-23 11:26:46
关于这个问题正在进行的讨论在哪里- https://github.com/alexanderbeletsky/elmah-mvc/pull/24。
目前,这是不可能的,但在票证中您可以看到几种解决方案,包括自定义过滤器。我仍然不确定是否需要在Elmah.MVC包中完成一些特殊的工作。
发布于 2013-08-20 12:46:21
您完全可以这样做,并重写ElmahController。事实上,贝列茨基已经提供了一个名为Elmah.Mvc的nuget包装。
一旦创建了自己的ElmahController,就可以对其应用任何授权。在我的应用程序中,我有一个基本的授权控制器来应用它。您只需要配置您的路由并返回Elmah结果,所有这些都会在他的站点上得到很好的记录。
更新:我已经有一段时间没有看过这个了,但是我有自己的控制器,受到上面的启发,但实际上并没有使用它。
[Authorize]
public class ElmahController : BaseAuthorizedController
{
public ActionResult Index(string type)
{
return new ElmahResult(type);
}
}结果是
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;
}
}
}我有两条路(第二条非常可选)
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 });https://stackoverflow.com/questions/18335243
复制相似问题