我有以下路线:
{语言}/{控制器}.mvc/{动作}/{id}
一旦用户选择了语言,就会以路由值语言进行维护。
http://localhost:4000/de/Account.mvc/注册 http://localhost:4000/de/Account.mvc/Register
如果用户点击需要授权的页面,我会遇到问题。抢劫然后重定向到http://localhost:4000/Account.mvc/Login?ReturnUrl=%2fde%2fAccount.mvc%2fProfileData http://localhost:4000/Account.mvc/Login?ReturnUrl=%2fde%2fAccount.mvc%2fProfileData
登录页面在 web.config 中配置,并且不允许使用来自路由的参数。登录后页面正常(http://localhost:4000/de/Account.mvc/ProfileData http://localhost:4000/de/Account.mvc/ProfileData)但登录页面本身没有路由值语言。
我怎样才能解决这个问题?
EDIT
我使用了 Darin 的答案,但必须包含原始授权过滤器 (AuthorizeAttribute.cs) 中的所有代码。原因记录在该文件中。它处理未经授权的用户可能从缓存获取安全页面的情况。
这是代码中的注释:
// ** IMPORTANT **
// Since we're performing authorization at the action level, the authorization code runs
// after the output caching module. In the worst case this could allow an authorized user
// to cause the page to be cached, then an unauthorized user would later be served the
// cached page. We work around this by telling proxies not to cache the sensitive page,
// then we hook our custom authorization code into the caching mechanism so that we have
// the final say on whether a page should be served from the cache.
表单身份验证的问题是您无法动态配置登录 URL。这正是 ASP.NET 团队设计框架的方式。有时会调用 FormsAuthentication.RedirectToLoginPage 方法,该方法将重定向到 web.config 中的硬编码 url。
我可以看到两种可能的解决方法:
- 不要将语言存储在 url 中,而是存储在 cookie 中
- 编写一个自定义 ActionFilter,如果用户未经过身份验证,它会重定向到动态构建的登录页面
以下是使用自定义属性的示例:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class RequiresAuthenticationAttribute : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
IPrincipal user = filterContext.HttpContext.User;
if (!user.Identity.IsAuthenticated)
{
filterContext.Result = new RedirectResult("CALCULATE YOUR LOGIN URL HERE FROM ROUTES");
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)