gpt4 book ai didi

c# - asp.net mvc 授权属性重定向

转载 作者:行者123 更新时间:2023-11-30 19:57:46 25 4
gpt4 key购买 nike

在我的应用程序中,我创建了一个这样的自定义属性

public class AdminAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var isAuthorized = base.AuthorizeCore(httpContext);
if (!isAuthorized || Auth.CurrentAdminUser == null)
{
return false;
}
else
{
return (SuperAdmin.Get(Auth.CurrentAdminUser.Id) != null) ? true : false;
}
}
}

它工作正常,但我想要的是根据用户是否未登录进行重定向,然后转到登录页面,如果用户已登录但不是 super 管理员,则让他不授权页。

现在发生的是所有未经授权的内容都通过 web.config 文件重定向到此页面,

<authentication mode="Forms">
<forms loginUrl="~/Site/NotAuthorize" timeout="2880" />
<!-- this is where we can set up that if you are not authenticated, where should you go then?-->
</authentication>

任何帮助将不胜感激。

最佳答案

你应该覆盖HandleUnauthorizedRequest

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
base.HandleUnauthorizedRequest(filterContext);

filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "xxx", action = "xxx", area = "" }));
}

关于c# - asp.net mvc 授权属性重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28744430/

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