Asp.net mvc 在Asp.net MVC中限制对控制器中操作方法的访问

Asp.net mvc 在Asp.net MVC中限制对控制器中操作方法的访问,asp.net-mvc,authentication,authorization,Asp.net Mvc,Authentication,Authorization,我是Asp.net MVC web开发的新手,我使用它开发了一个应用程序。在我的应用程序中,我使用自己的身份验证和授权检查,如下所示: 我在创建的登录操作方法中创建了登录控制器,如下所示 [HttpPost] public ActionResult Login(LoginViewModel Info) { if (ModelState.IsValid) { if (checking username and password exist in DB or not)

我是Asp.net MVC web开发的新手,我使用它开发了一个应用程序。在我的应用程序中,我使用自己的身份验证和授权检查,如下所示: 我在创建的登录操作方法中创建了登录控制器,如下所示

[HttpPost]
public ActionResult Login(LoginViewModel Info)
{
    if (ModelState.IsValid)
    {
        if (checking username and password exist in DB or not)
        {
            //Adding required values in session 
            Session["username"] = Info.Username;

            //Redirect to dashboard     
        }
        else
        {
            //not found redirect to login page
        }
    }
    return View();
}
public class CustomAuthorize : AuthorizeAttribute
{
    // Custom property
    public string ValidRole { get; set; }

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext.Session["username"] == null)
        {
            //User is not logged-in so redirect to login page
            return false;
        }
    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        filterContext.Result = new RedirectToRouteResult(
        new RouteValueDictionary(
            new
            {
                controller = "Login",
                action = "Login"
            })
        );      
    }
}
现在,当访问管理控制器中的操作方法时,我使用我的“自定义授权”属性来检查用户是否登录,以及是否拥有该方法的权限

public class AdminController : Controller
{
    [CustomAuthorize(ValidRole = "Admin")]
    public ActionResult Index()
    {
        return View();
    }
}
为此,我覆盖如下默认属性

[HttpPost]
public ActionResult Login(LoginViewModel Info)
{
    if (ModelState.IsValid)
    {
        if (checking username and password exist in DB or not)
        {
            //Adding required values in session 
            Session["username"] = Info.Username;

            //Redirect to dashboard     
        }
        else
        {
            //not found redirect to login page
        }
    }
    return View();
}
public class CustomAuthorize : AuthorizeAttribute
{
    // Custom property
    public string ValidRole { get; set; }

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext.Session["username"] == null)
        {
            //User is not logged-in so redirect to login page
            return false;
        }
    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        filterContext.Result = new RedirectToRouteResult(
        new RouteValueDictionary(
            new
            {
                controller = "Login",
                action = "Login"
            })
        );      
    }
}
这个代码对我来说很好用。我的问题是,有没有更好的解决方案来检查用户是否登录,并根据它将用户重定向到登录或仪表板页面,这样用户就不能操纵url并访问未经授权的功能

提前谢谢

我的问题是,有没有更好的办法来检查 用户是否登录,并根据它重定向用户登录或 仪表板页面,用户无法操纵url并访问 他未被授权使用的功能

是的,已经有一个内置的方法来执行此操作,它不依赖于ASP.NET会话。它被称为

您不需要编写任何自定义授权属性。验证用户的凭据后,只需设置FormsAuthenticationCookie:

if (checking username and password exist in DB or not)
{
    // Emitting forms authentication cookie
    FormsAuthentication.SetAuthCookie(Info.Username, false);

    //Redirect to dashboard     
}
然后只需使用内置的Authorize属性来修饰受保护的控制器操作:

public class AdminController : Controller
{
    [Authorize(ValidRole = "Admin")]
    public ActionResult Index()
    {
        // At this stage the user is authenticated and has the role Admin.
        // You could get the current username using the User.Identity.Name property
        return View();
    }
}
表单身份验证是无状态的。它不依赖服务器上的任何状态来跟踪服务器上当前经过身份验证的用户。关于当前用户的信息包含在加密的表单身份验证cookie中,该cookie随每个请求一起发送。这样,当应用程序托管在web场中时,您就不必考虑处理复杂的场景,在这种情况下,您需要使用分布式ASP.NET会话