登录失败IIS登录(Windows身份验证)

时间:2015-12-30 07:59:03

标签: c# authentication iis

在我们的应用程序中,我们提供Windows身份验证的使用。为此,浏览器会弹出一个窗口。当用户正确登录时,我可以在此后的代码中将其记录下来:

 if (HttpContext.Current.User.Identity.IsAuthenticated) c.Response.Redirect(General.PathPrefix() + "Default.aspx");

我的问题是,如何记录未经过身份验证的用户,所有代码似乎在登录失败时停止,我得到401回复。

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码在Global.asax文件中记录此错误

public class CoolMvcApplication : System.Web.HttpApplication
{
    protected void Application_Error(object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();
        //check here if this exception is an unauthorized access error, if so
        Server.ClearError();
        Response.Redirect("/not_loggedin");
    }
}

如果错误是未经授权的访问并遵循相同的逻辑,您也可以通过使用过滤器并在那里断言来实现此目的。