【问题标题】:How to set a custom route value so that only members with the Admin role can access my elmah.axd page in MVC3如何设置自定义路由值,以便只有具有管理员角色的成员才能访问我在 MVC3 中的 elmah.axd 页面
【发布时间】:2013-03-06 16:23:19
【问题描述】:

我需要设置一个自定义路由值,这样你就不得不去

网站/管理员/elmah.axd

在我的 web.config 我有:

<location path="elmah.axd">
 <system.web>
   <authorization>
     <allow roles="Admin" />
     <deny users="*" />
   </authorization>
 </system.web>
</location>

加上所有其他相关的 elmah 设置。如果我删除该部分,我可以输入:

网站/elmah.axd

但我只希望管理员能够访问此页面。

我有一个您登录的管理页面,如果您是管理员角色,那么您应该被重定向到 elmah.axd

    public AdminController(IRepository<User> userRepository)
    {
        _userRepository = userRepository;
    }

    //
    // GET: /Admin/

    public ActionResult Index()
    {
        return View(Constants.Admin);
    }

public ActionResult AdminLogin(string SSN)
    {
        var user = _userRepository.FindBy(x => x.UserName == SSN).SingleOrDefault();
        if (UserIsAnAdmin(user))
        {
            return RedirectToRoute("/elmah.axd");
        }
        return View(Constants.DeniedAccess);
    }

我的 Global.asax 文件中有一个路由值需要帮助。

 routes.MapRoute(
            "mocklogin",
            "{controller}/{action}", // URL with parameters
            new { controller = "MockLogin", action = "Index" } // Parameter defaults
            );


        routes.MapRoute(
            "elmah.axd",
            "{controller}/{action}", // URL with parameters

            );



        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}", // URL with parameters
            //need something here redirecting to the elmah.axd page      
    );

有没有办法将页面重定向到我网站上的 /elmah.axd 页面。

最后我想去我网站的管理员页面,当我点击提交时,如果用户是管理员,我想重定向到 elmah.axd 页面。否则我会转到我的自定义错误页面。我需要以某种方式让控制器上的 ssn 调用条件,如果为真,则重定向到 elmah.axd。如何在 MVC 中重定向到 elmah.axd?

【问题讨论】:

  • 我不确定你想在这里做什么,你想让默认路由重定向到 elmah.axd 吗?那么安全性会由web.config来处理吗?什么现在有效,您是否正在寻求帮助?
  • 如果您保留 设置并转到 AdminLogin,输入您的凭据,是否有效?你在路线上还有什么?如果您使用位置设置转到 /elmah.axd 路径,会发生什么?
  • 我收到错误:路由表中没有路由与提供的值匹配。在上面的路线上,我需要最后一行,而不是路由到控制器和操作,我需要呈现 elmah.axd 页面
  • 你能发布你所有的路线配置吗?还有你的完整控制器
  • 不知道你想做什么,但这可能会有所帮助:code.google.com/p/elmah/wiki/DotNetSlackersArticle

标签: asp.net-mvc-3 elmah


【解决方案1】:

更新:要重定向到静态路由,请使用 Response.Redirect("URL");

见下文:

您不能创建指向控制器和动作以外的其他东西的路由。所以最好的办法是你的控制器的索引动作重定向到 /elmah.axd 并将你的默认路由设置为这个动作。

路线:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}", // URL with parameters

);

控制器:

    public AdminController(IRepository<User> userRepository)
    {
        _userRepository = userRepository;
    }

    public ActionResult Index()
    {
         Response.Redirect("/elmah.axd");
         return View(); //will not be hit
    }

    public ActionResult AdminLogin(string SSN)
    {
        var user = _userRepository.FindBy(x => x.UserName == SSN).SingleOrDefault();
        if (UserIsAnAdmin(user))
        {
            return RedirectToRoute("/elmah.axd");
        }
        return View(Constants.DeniedAccess);
    }

让我知道它是否有效。

【讨论】:

  • 不太好用,让我改写我的问题,也许会更清楚
【解决方案2】:

您可以在Global.asax 文件中使用Application_AuthenticateRequest

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    if (HttpContext.Current.User.IsInRole("Admin"))
        // Do your thing
}

【讨论】:

  • 这只适用于 MVC 提供的默认身份验证对吗?
  • 我目前正在使用第三方应用对用户进行身份验证
  • 这取决于。我假设第三方应用程序仍将使用部分身份验证框架。否则我对安全性会不太满意。
猜你喜欢
  • 2021-08-18
  • 1970-01-01
  • 2020-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多