简体   繁体   中英

Log failed IIS login (windows authentication)

In our application, we offer the use of Windows authentication. For this, the browser pops-up a window. When a user is logged in correctly, I can log this in the code after this:

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

My question is, how can I log a user not getting authenticated, all code seems to stop when the login fails, and I get a 401 response.

You can log this error in the Global.asax file using the code below

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");
    }
}

Also you can achieve this by using filters and assert there if the error is unauthorized access and follow the same logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM