使用MVC自定义身份验证

时间:2016-12-07 06:44:15

标签: c# asp.net-mvc asp.net-mvc-3 form-authentication

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

Scenerio ::用户将有一个登录表单,他将输入userId和密码。            然后根据我的业务逻辑验证这些凭据。我不能使用默认进程,比如从User表验证然后分配角色等。

我的业务逻辑将为我提供一个值为IsValidUser和UserRole的虚假值。

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

1 个答案:

答案 0 :(得分:3)

对于自定义身份验证,您可以使用" ActionFilters"用于授权特定控制器的类,您可以传递从"商务逻辑"中获得的参数。以财产的形式,

行动方法,

[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);
    }
}
相关问题