我不知道我完全理解你正在嘗試做的,但我想你想將用戶重定向到不同的索引頁面一旦登錄? 如果是這樣,你有幾種選擇: 假設你正在使用的標識模型運與MVC5:
1 - 在您的的AccountController - 登錄行動(HttpPost)
後VAR的結果=等待SignInManager.PasswordSignInAsync .... 添加類似的東西:
- var user = await UserManager.FindAsync(model.Email,model.Password);
- returnUrl = UserManager.IsInRole(user.Id,「Admin」)? 「/ Admin/Home」: returnUrl;
2 - 或者你可以創建一個客戶ActionFilterAttribute像這樣的(簡化演示目的,但尚未工作示例):
public class RedirectLoginFilter:ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
// First check if authentication succeed and user authenticated:
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
bool IsAdmin = filterContext.HttpContext.User.IsInRole("Admin");
//Then check for user role(s) and assign view accordingly, don't forget the
//[Authorize(Roles = "YourRoleHere")] on your controller/action
if (IsAdmin)
{
filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary
(new
{
area = "Admin",
controller = "Home",
action = "Index"
}));
}
else
{
filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary
(new
{
area = "",
controller = "Home",
action = "Index"
}));
}
}
base.OnActionExecuted(filterContext);
}
現在在默認RETURNURL控制器操作的所有非標識的用戶,即:首頁/索引加入您的自定義過濾器行動屬性:
public class HomeController : Controller
{
[RedirectLoginFilter]
public ActionResult Index()
{
return View();
}
請記住,使用最後一個解決方案時,您每次嘗試使用管理角色訪問您的Home/Index方法時,您都將被重定向到管理索引頁面。
我會試試你的解釋。非常感謝:D –