![](/img/trans.png)
[英]catching exception thrown By CustomAuthorizeAttribute ASP.NET MVC 4
[英]CustomAuthorizeAttribute redirects to LoginView even when the user is logged in (ASP.Net MVC 5)
對於我的學習,我必須實現一個自定義的AuthorizeAttribute,以允許或禁止用戶根據其角色(管理員,經理等)訪問應用程序的各個部分。 我正在使用自己的SQL DB。
您能否幫助我了解為什么即使用戶登錄也將其重定向到LoginView? 我在網上搜索了很多內容,但沒有找到任何解決方案。
Web.config:
<authentication mode="Forms">
<forms loginUrl="~/Authentication/Login" timeout="2880" />
</authentication>
AuthenticationController:
public class AuthenticationController : Controller
{
[AllowAnonymous]
public ActionResult Login()
{
return View();
}
[HttpPost]
[AllowAnonymous]
public ActionResult Login(User user, string returnUrl)
{
if (ModelState.IsValid)
{
if (IsValid(user.Email, user.Password))
{
FormsAuthentication.SetAuthCookie(user.Email, true);
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "Incorrect Email or Password.");
return View(user);
}
}
return View(user);
}
public ActionResult Logout()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
private bool IsValid(string email, string password)
{
var crypto = new SimpleCrypto.PBKDF2();
bool isValid = false;
using (var dBContext = new DBContext())
{
var user = dBContext.Users.FirstOrDefault(u => u.Email == email);
if (user!=null)
{
if (user.Password == crypto.Compute(password, user.saltedPassword))
{
isValid = true;
}
}
}
return isValid;
}
}
家庭控制器:
public class HomeController : Controller
{
[CustomAuthorize(Roles = "Admin,Manager,...")]
public ActionResult Index()
{
return View();
}
}
過濾:
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
public CustomAuthorizeAttribute() { }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (!httpContext.Request.IsAuthenticated)
return false;
DBContext dBContext = new DBContext();
var roleList = from r in dBContext.Roles
join rl in dBContext.RoleLists on r.RoleId equals rl.RoleId
join s in dBContext.Subscriptions on rl.SubscriptionId equals s.SubscriptionId
join u in dBContext.Users on s.UserId equals u.UserId
where u.Email == httpContext.User.Identity.Name
select r.RoleName;
foreach (string definedRole in this.Roles.Split(','))
{
foreach (string role in roleList)
{
if (definedRole.Equals(role))
{
return true;
}
}
}
return false;
}
FilterConfig:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new CustomAuthorizeAttribute());
filters.Add(new HandleErrorAttribute());
}
}
編輯以添加更多信息:
如果我使用AuthorizeCore()的返回值true:在foreach的第一個迭代中,Roles不包含定義的角色,但是在第二個迭代中...系統顯示請求的視圖,而沒有采用定義的角色考慮。
如果我將AuthorizeCore()的返回值用作false:第一個foreach的迭代僅發生一次,並且此。Roles不包含已定義的角色,第二個foreach將用戶的角色與“” ...進行比較系統顯示LoginView。
我通過注釋以下行來解決此問題:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//filters.Add(new CustomAuthorizeAttribute());
filters.Add(new HandleErrorAttribute());
}
}
現在,已登錄用戶的角色與在
[CustomAuthorize(角色=“ Admin,Manager,...”)]
應用程序顯示請求的視圖,除非用戶沒有權限。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.