响应在此上下文中不可用 Global.asax
Response is not Available in this context Global.asax
我正在检查用户是否已经在 global.asax 中获得授权,如果为真则重定向到某个路由
if (false)
{
HttpContext.Current.Response.RedirectToRoute("Login");
}
它抛出异常:
Response is not available in this context
我认为在 web.config
中使用 authentication tag 会是更好的解决方案。
// or loginUrl="~/Account/LogOn" for example in an MVC application
<authentication mode="Windows">
<forms
name=".ASPXAUTH"
loginUrl="login.aspx"
defaultUrl="default.aspx"
protection="All"
timeout="30"
path="/"
requireSSL="false"
slidingExpiration="true"
cookieless="UseDeviceProfile" domain=""
enableCrossAppRedirects="false">
<credentials passwordFormat="SHA1" />
</forms>
<passport redirectUrl="internal" />
</authentication>
您可以定义一个 loginUrl,如果用户试图访问需要身份验证的资源,用户将被重定向。
更新
根据您给出的评论,我认为您可能正在寻找基于授权的路由。在这个 SO 问题 MVC role-based routing.
中已经有了答案
据我所知,ASP.NET 与 HttpRequest
和 HttpResponse
对象一起创建了 HttpContext
对象。它发生在创建 HttpApplication
实例之前。
看来 HttpContext.Current
只是在这个阶段行不通。
在应用程序事件的处理程序中,您可以获得抛出 sender
:
的上下文
private void OnAuthorizeRequest(object sender, EventArgs e)
{
var application = (HttpApplication)sender;
var context = (HttpContext)application.Context;
}
(AuthorizeRequest
是重定向匿名用户的正确位置,因为之前的 AuthenticateRequest
已通过身份验证 或 尚未通过身份验证 用户已经。)
查看详情here。
重要:邻居回答绝对正确,你应该用web.config
做这个东西。我对 "how it works" 和 "how it could be done".
的回答
我正在检查用户是否已经在 global.asax 中获得授权,如果为真则重定向到某个路由
if (false)
{
HttpContext.Current.Response.RedirectToRoute("Login");
}
它抛出异常:
Response is not available in this context
我认为在 web.config
中使用 authentication tag 会是更好的解决方案。
// or loginUrl="~/Account/LogOn" for example in an MVC application
<authentication mode="Windows">
<forms
name=".ASPXAUTH"
loginUrl="login.aspx"
defaultUrl="default.aspx"
protection="All"
timeout="30"
path="/"
requireSSL="false"
slidingExpiration="true"
cookieless="UseDeviceProfile" domain=""
enableCrossAppRedirects="false">
<credentials passwordFormat="SHA1" />
</forms>
<passport redirectUrl="internal" />
</authentication>
您可以定义一个 loginUrl,如果用户试图访问需要身份验证的资源,用户将被重定向。
更新
根据您给出的评论,我认为您可能正在寻找基于授权的路由。在这个 SO 问题 MVC role-based routing.
中已经有了答案据我所知,ASP.NET 与 HttpRequest
和 HttpResponse
对象一起创建了 HttpContext
对象。它发生在创建 HttpApplication
实例之前。
看来 HttpContext.Current
只是在这个阶段行不通。
在应用程序事件的处理程序中,您可以获得抛出 sender
:
private void OnAuthorizeRequest(object sender, EventArgs e)
{
var application = (HttpApplication)sender;
var context = (HttpContext)application.Context;
}
(AuthorizeRequest
是重定向匿名用户的正确位置,因为之前的 AuthenticateRequest
已通过身份验证 或 尚未通过身份验证 用户已经。)
查看详情here。
重要:邻居回答绝对正确,你应该用web.config
做这个东西。我对 "how it works" 和 "how it could be done".