gpt4 book ai didi

c# - 使用 MVC 自定义身份验证

转载 作者:太空宇宙 更新时间:2023-11-03 23:06:35 24 4
gpt4 key购买 nike

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

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

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

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

最佳答案

对于自定义身份验证,您可以使用“ActionFilters”类来授权特定的 Controller ,并且您可以以属性的形式传递从“业务逻辑”中获得的那些参数,

Action 方法,

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

Action Filter 就像

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

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

关于c# - 使用 MVC 自定义身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41011032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com