如果用户已登录,我想显示我的部门视图,如果未登录则要显示登录页面。我在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 }
);
}
}
但是这个人总是在启动时加载登录页面。任何人都可以在这里指出我做错了什么? 注意:我在此应用程序中使用Asp.net Identity
答案 0 :(得分:5)
您的HttpContext.Current.User==null
逻辑将进入控制器,而不是您的路线注册
注意 - 正确的电话是Request.IsAuthenticated
假设你有一个这样的动作方法:
public ViewResult Index()
{
if(Request.IsAuthenticated)
return new RedirectResult("toLoginPage")
else
return new View("loggedInView");
}
但是,我相信 [Authorize]
属性可能就是您在用例中所需的内容:(注意 - 重新阅读问题,这可能不准确,因为你想根据登录状态返回不同的视图)
[Authorize]
public ViewResult ShowPerson(int id)
{
return new View("loggedInView");
}
在你的web.config中,类似
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" />
</authentication>
</system.web>
在此特定实例中,如果操作方法上方有[Authorize]
属性,如果用户未登录,则会将其重定向到登录。
答案 1 :(得分:1)
创建自己的授权属性:
public class CustomAuthorize: AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if(filterContext.HttpContext.User.Identity.IsAuthenticated)
{
base.HandleUnauthorizedRequest(filterContext);
}
else
{
filterContext.Result = new RedirectToRouteResult(new
RouteValueDictionary(new{ controller = "Error", action = "AccessDenied" }));
}
}
}
然后将 [CustomAuthorize] 添加到您的控制器并更改它指向的路线。
这取自here
答案 2 :(得分:1)
您可以通过路线限制来实现这一目标:
public class DelegateConstraint : IRouteConstraint
{
private readonly Func<HttpContextBase, bool> _isMatch;
public DelegateConstraint(Func<HttpContextBase, bool> isMatch)
{
_isMatch = isMatch;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return _isMatch(httpContext);
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "CustomAuth1",
url: "AuthArea/{action}/{id}",
defaults: new { controller = "Department", action = "Index", id = UrlParameter.Optional },
constraints: new { auth = new DelegateConstraint(httpContext => !httpContext.Request.IsAuthenticated) }
);
routes.MapRoute(
name: "CustomAuth2",
url: "AuthArea/{action}/{id}",
defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional },
constraints: new { auth = new DelegateConstraint(httpContext => httpContext.Request.IsAuthenticated) }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
在此示例中,帐户或部门控制器将解析〜/ AuthArea网址,具体取决于Request.IsAuthenticated属性。
更新: 这样您就可以获得完整的路由功能,但仍需要指定正确的控制器:
@Html.ActionLink("Context dependent link", "Index", @Request.IsAuthenticated ? "Account" : "Department")
此链接将始终显示为:
<a href="/AuthArea">Context dependent link</a>