C#C
C#3y ago
7 replies
Whiteboy

ASP.NET HTTP ERROR 401 after deleting cookies

So i need to login users using authorization prompt and just get their logins, but when i tried to make a logout and cleared cookie data on site to try logging in with other account it stopped working (I get http error 401)

Edit: I tested with other browser and the login works just file but i remain logged in even after not saving credentials and reopening the page. How do i fix it?

    public class IndexModel : PageModel
    {
        public string Username { get; private set; }

        public IActionResult OnGet()
        {
            string authHeader = Request.Headers["Authorization"];

            if (!string.IsNullOrEmpty(authHeader) && authHeader.StartsWith("Basic "))
            {
                string base64Credentials = authHeader.Substring("Basic ".Length).Trim();
                string credentials = Encoding.UTF8.GetString(Convert.FromBase64String(base64Credentials));

                string[] parts = credentials.Split(':');
                Username = parts[0];

                return Page();
            }

            // If no valid authentication header, return a challenge response to prompt for credentials
            Response.Headers["WWW-Authenticate"] = "Basic realm=\"My Realm\"";
            return new UnauthorizedResult();
        }
    }
Was this page helpful?