使用 MVC 自定义身份验证

Custom authentication with MVC

我想使用 MVC3 创建自定义身份验证。

场景::用户将有一个登录表单,他将在其中输入用户名和密码。 然后根据我的业务逻辑验证这些凭据。我不能使用默认流程,比如从用户 table 验证它然后分配角色等。

我的业务逻辑将为我提供布尔值 IsValidUser 和 UserRole。

问题:: 我需要做两件事:
1. 在 AuthoriseAttribute 过滤器中使用业务逻辑返回的这些值。这样我就可以限制用户访问控制器的任何特定操作。(我尝试将这些值放入会话变量中,但无法在授权过滤器中使用它们。)
2.如何使用Formsauthentication.Setauthcookie来完成这个任务。

对于自定义身份验证,您可以使用 "ActionFilters" class 来授权特定控制器,并且您可以以 属性 的形式传递从 "buisness logic" 获得的那些参数,

操作方法,

[MyActionFilter(Property1 = "Value1", Property2 = "Value2")]
public ActionResult Index()
{
    return View();
}

动作过滤器就像

public class MyActionFilter : ActionFilterAttribute
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
    }
}