当应用需要重定向到登录页面时,使用 Forms Authentication 是否有事件或任何扩展点可以让我在重定向到登录页面之前对请求做额外的工作?
我想在查询字符串中发送额外的信息,这些信息可能会有所不同,因此仅将其静态嵌入到 web.config 中 loginUrl 节点的链接中是行不通的。
编辑:为了澄清,我想在被重定向到登录页面之前拦截请求。
示例:
<authentication mode="Forms">
<forms loginUrl="http://the/interwebs/login.aspx" timeout="2880"
enableCrossAppRedirects="true" />
</authentication>
并且在用户被重定向到 http://the/interwebs/login.aspx 之前我希望能够打包查询值,以便 url 最终可以是 http://the/interwebs/login.aspx?Action=Refresh 之类的东西
最佳答案
您可以通过处理事件 HttpApplication.AuthenticateRequest 来做到这一点
private void Application_AuthenticateRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = context.Request.Cookies[cookieName];
if (authCookie == null || string.IsNullOrEmpty(authCookie.Value))
{
//... do something
}
仅供引用,我们目前通过 IHttpModule 实现来执行此操作。
关于c# - Forms Authentication 添加附加信息以及 ReturnUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3919495/