gpt4 book ai didi

asp.net-mvc-4 - 如何将角色不正确的用户重定向到“不允许”?

转载 作者:行者123 更新时间:2023-12-05 00:29:14 26 4
gpt4 key购买 nike

在 MVC 4 项目中使用 SimpleMembershipProvider 时,当没有适当角色的用户运行操作时,他被重定向到帐户/登录。如何将用户重定向到我自己的“没有足够的权限查看页面”操作?

最佳答案

你可以覆盖方法来做到这一点

public class CustomAuthorizeAttribute : AuthorizeAttribute {
public override void OnAuthorization(AuthorizationContext filterContext) {
base.OnAuthorization(filterContext);

if (filterContext == null) {
throw new ArgumentNullException("filterContext");
}

if ({your code detecting no user is logged}) {
filterContext.Result = new RedirectResult(System.Web.Security.FormsAuthentication.LoginUrl + "?returnUrl=" + filterContext.HttpContext.Server.UrlEncode(filterContext.HttpContext.Request.RawUrl));
return;
}

if ({your code detecting that the user has no access}) {
var ViewData = new ViewDataDictionary();
ViewData.Add("Title", "No access");
ViewData.Add("Description", "blah blah blah blah blah blah blah ");
filterContext.Result = new ViewResult { ViewName = "~/Views/Shared/NoAccess.cshtml", ViewData = ViewData };
}

}

关于asp.net-mvc-4 - 如何将角色不正确的用户重定向到“不允许”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17680768/

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