c# - 通过 ActionFilter 检查 session 是否存在

标签 c# asp.net asp.net-mvc-3

我有一个项目,我没有使用默认模板构建,而是从头开始。

现在我需要为管理员实现检查,但我不认为

public ActionResult someAction()
{
   if (session exists)
   {
      // do it
   }
   else
   {
      //redirect back or show 403
   }
}

是每个删除/编辑/创建操作的好主意。

我想做的是构建操作过滤器,它将检查管理 session 是否存在,如果没有 session ,它将重定向到 403 或类似的东西。

[AdminCheck]   
public ActionResult someAction()
{
   // do it
}

但是我不知道该怎么做。我做了一些研究并提出了它,但我不知道如何在其中实现功能。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace stevePortfolio.Infrastructure
{
    public class AdminCheck : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            // No idea what to write here...
        }
    }
}

最佳答案

你应该使用 AuthorizeAttribute为了这。您可以立即使用它来检查用户是否是特定角色的成员,如下所示:

[Authorize(Roles = "IsAdmin")]   
public ActionResult DoStuff()
{
   //action body
}

或者,如果您需要更多的复杂性并放置在所需的代码中,您可以将其子类化。

public class AuthorizeByRightAttribute : AuthorizeAttribute
    {
       protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
        {
            var authorized = base.AuthorizeCore(httpContext);
            if (authorized && controller != null)
            {
                //Return true or false based on some criteria
            }

        }

您可以随心所欲地处理未经授权的请求。下面的示例通过发出一个 HTTP 状态代码 403 和一个 jsonresult 来让我的 ajax 方法检查,或者对于正常的 http 请求,重定向到“未授权”页面。

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            if (!controller.PortalSession.ValidSession)
            {
                base.HandleUnauthorizedRequest(filterContext);
            }
            else
            {
                if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
                {
                    //base.HandleUnauthorizedRequest(filterContext);
                    filterContext.RequestContext.HttpContext.Response.StatusCode = 403;
                    var result = new JsonResult();
                    result.Data = new {Success=false};
                    result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                    filterContext.Result = result;
                    return;
                }

                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary
                        {
                            {"controller", MVC.Login.Name},
                            {"action", MVC.Login.ActionNames.NotAuthorized},
                            {"group", RequiredRole}
                        });
            }


        }

关于c# - 通过 ActionFilter 检查 session 是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6931991/

相关文章:

c# - 如何通过从其他表单获取值来重置用户控件

javascript - 无法获取 asp :textbox on external javascript file 的值

asp.net - 在企业 ASP.net 应用程序中编写 DTO 层的最佳实践是什么?

javascript - Gmail OpenID 用户无法识别

.net - 如何在 MVC3 Controller 中处理存储库对象

c# - Unity - 覆盖游戏对象的位置属性

c# - EWS 邮箱组流订阅

c# - 如何使用 Selenium Webdriver (C#) 在 Chrome 中处理浏览器身份验证?

C# FrameworkCore 和 MySQL : Cannot delete or update a parent row: a foreign key constraint fails

c# - 如何替换某些段的值以保留其他段的值?