ASP.NET MVC 5 Authentication Filters改进了基于过滤器的身份验证

文章主要介绍如何在ASP.NET MVC5中使用新的Authentication Filters基于过滤器的身份验证机制。

ASP.NET MVC 5包含在最近发布的Visual Studio 2013开发者预览版中。

如果你在之前在项目中使用过MVC,你一定非常喜欢通过Authorization属性来为你的网站加强基于角色的安全验证的方式。在MVC5中,开发人员可以实现身份验证过滤器,它可以提供使用各种第三方供应商或自定义的身份验证机制进行用户身份验证的能力。

注意:Authentication的验证级别高于任何Authorization验证.

接下来我们在实践中应用一下Authorization, 首先创建一个web工程如图1

ASP.NET MVC 5 Authentication Filters

图片1:新建Web应用工程
(点击图片查看大图)

选择MVC工程如图2

ASP.NET MVC 5 Authentication Filters

图2:选择MVC工程
(点击图片查看大图)

工程建好之后,我们来看看如何通过Authentication 过滤器实现简单的用户未登录返回登录界面功能. 首先在你的工程中建立CustomAttributes文件夹, 接下来在文件夹下面建立一个名字为CustomAttribute的类, 并且使它继承ActionFilterAttribute和IAuthenticationFilter:

public class BasicAuthAttribute: ActionFilterAttribute, IAuthenticationFilter

IAuthenticationFilter接口定义了两个方法分别是:OnAuthentication和OnAuthenhenticationChallenge. OnAuthentication方法首先被执行,并且可以包含任何所需要的验证. OnAuthenhenticationChallenge方法用来限制已通过验证用户的行为.

对于这个简单的例子,这里只实现OnAuthenticationChallenge方法. 留白OnAuthentication方法.

public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
{
    var user = filterContext.HttpContext.User;
    if (user == null || !user.Identity.IsAuthenticated)
    {
        filterContext.Result = new HttpUnauthorizedResult();
    }
}

 下面是整个BasicAuthAttribute实现的代码:

using System.Web.Mvc;
using System.Web.Mvc.Filters;
 
namespace VSMMvc5AuthFilterDemo.CustomAttributes
{
    public class BasicAuthAttribute : ActionFilterAttribute, IAuthenticationFilter
    {
        public void OnAuthentication(AuthenticationContext filterContext)
        {
        }
 
        public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
        {
            var user = filterContext.HttpContext.User;
            if (user == null || !user.Identity.IsAuthenticated)
            {
                filterContext.Result = new HttpUnauthorizedResult();
            }
        }
    }
}

现在你可以在HomeController中使用并测试BasicAuthAttribute. 打开HomeController文件, 并且为它添加CustomAttributes命名空间的引用.

using VSMMvc5AuthFilterDemo.CustomAttributes;

然后应用自定义属性到HomeController:

[BasicAuthAttribute]
public class HomeController : Controller

现在当你编译并启动工程后, 应该会自动重定向到登陆页面:图3

ASP.NET MVC 5 Authentication Filters

图3:登录界面
(点击查看大图)

想要查看首页, 你需要注册一个用户账户:图4

ASP.NET MVC 5 Authentication Filters

图4:注册页面
(点击查看大图)

完成注册之后, 你将会自动重定向到首页:图5

ASP.NET MVC 5 Authentication Filters

图5:首页
(点击查看大图)

如你所见, 在MVC5中实现自定义的Authentication Filters并不是非常复杂. 目前这方面的文档非常少, 但是我可遇见到它能很好的应用在用户审核和日志记录方面, 或者使用自定义属性实现根据用户的认证提供商或身份标识提供给用户不同的访问权限的类似功能.

本文章翻译自:http://visualstudiomagazine.com/articles/2013/08/28/asp_net-authentication-filters.aspx翻译:天屹ASP.NET MVC 5 Authentication Filters 原文作者:Eric Vogel



2 评论

  1.   •  

    請問一下,如何在 asp.net 的mvc5中 把一個數值設置成 如系統提供的 “User.Identity.GetUserName()” 這個一樣,不管是在 “視圖” 和 “控制器” 裏面都能使用!

    • 天屹   •     作者

      没有已有的方法,只能放在session或者cookie里面, 或者是这个值是存在数据库的从数据库中取出来放在缓存里面,写一个方法方法里面第一次从数据库中读取,以后都从缓存里面拿。

天屹进行回复 取消回复

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>