How to implement basic HTTP authentication with the VS debug server?

前端 未结 2 1250
野趣味
野趣味 2021-01-14 14:22

I\'m making a test rig for an ActiveX HTTP control, and I need to create a web site to securely POST to. To keep things simple, I\'m running the web app with the VS debug se

2条回答
  •  执笔经年
    2021-01-14 15:10

    michielvoo's answer is great, but for sheer simplicity, I went with this in the code for the page:

    string authorization = Request.Headers["Authorization"];
    string userInfo;
    string username = "";
    string password = "";
    if (authorization != null)
    {
         byte[] tempConverted = Convert.FromBase64String(authorization.Replace("Basic ", "").Trim());
         userInfo = System.Text.Encoding.UTF8.GetString(tempConverted);
         string[] usernamePassword = userInfo.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
         username = usernamePassword[0];
         password = usernamePassword[1];
    }
    
    if (username == "yourusername" && password == "yourpassword")
    {
    }
    else
    {
         Response.AddHeader("WWW-Authenticate", "Basic realm=\"Test\"");
         Response.StatusCode = 401;
         Response.End();
    }
    

提交回复
热议问题