在我的ASP.NET MVC应用程序中,我有大多数控制器装饰
[Authorize(Roles="SomeGroup")]
Run Code Online (Sandbox Code Playgroud)
当用户无权访问某些内容时,会将其发送到"〜/ Login",这是我的帐户控制器上的"登录"操作.
如何确定用户因未获得授权而已到达登录页面,以便我能够显示相应的错误?
我有一个使用ASP.Net MVC3并使用角色成员资格的项目.我在每个控制器中使用授权.例如:
[Authorize(Roles = "Administrator")]
public ActionResult Index(string q, int i)
{
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
如果某人没有管理员角色,则默认情况下会重定向到登录页面.如何更改它,所以它将重定向到Views/Shared/UnAuthorize.cshtml?或者如果有人没有管理员的角色,它会显示消息框(警告)?
提前致谢.
redirect asp.net-membership roles unauthorized asp.net-mvc-3
如果用户已登录,我想显示我的部门视图,如果未登录则要显示登录页面.我在我的内心尝试过类似的东西RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
if (HttpContext.Current.User==null)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}
else
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Department", action = "Index", id = UrlParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
但这个人总是在启动时加载登录页面.任何人都可以指出我在这里做错了什么?注意:我正在为此应用程序使用Asp.net Identity
我正在研究asp.net实体框架.我想只通过登录访问页面,而不是直接输入页面的URL.如果用户输入页面的URL并尝试访问它,则应将其重定向到登录页面.我怎样才能做到这一点?用户应该使用DB的用户名和密码登录.我是ASP.NET的新手