Share via

USER ID AND PASSWORD USING API

Anonymous
2023-05-13T16:19:26.9166667+00:00

I have code hardcoded in program, I want to change using the below api, where I store userid and password

https://sheet2api.com/v1/CAdt0QU7TXCy/untitled-spreadsheet

Please suggest

    protected void Button1_Click(object sender, EventArgs e)
        {
            string uid = TextBox1.Text;
            string pass = TextBox2.Text;
            string outpass;

            IDictionary<string, string> credentials = new Dictionary<string, string>();
  credentials.Add("user", "pass@0523"); //adding a key/value using the Add() method
          credentials.Add("harshit", "bala@2012");
           credentials.Add("happy", "tree");
            credentials.Add("tcs", "chn@1");
            credentials.Add("kaiser", "cigna");
            Session["fromlogin"] = "";
            if (!credentials.ContainsKey(uid))
            {
                Label4.Text = "Invalid Userid.!!";
            }
          else
            {
                if(credentials.TryGetValue(uid, out string password))
                {
                    outpass = password;

                    if(outpass!=pass)
                    { Label4.Text = "Invalid Password.!!"; }
                    else
                    { Session["fromlogin"] = "Y";
                        Response.Redirect("Main.aspx");

                    }

                }
                

            }

          
          
          


        }
Developer technologies | ASP.NET Core | Other
Developer technologies | ASP.NET Core | ASP.NET API

Answer accepted by question author

  1. Lan Huang-MSFT 30,211 Reputation points Microsoft External Staff
    2023-05-16T02:53:47.0466667+00:00

    Hi @KALYANA ALLAM,

    You can use JsonConvert.SerializeObject to serialize a query into a JSON string.

    string jsonData = JsonConvert.SerializeObject(query);

    Because your API has a pair of square brackets, you need to remove the square brackets to match.

    var a = strJSON.Substring(1, strJSON.Length - 2);

     protected void Button1_Click(object sender, EventArgs e)
            {
                string uid = TextBox1.Text;
                string pass = TextBox2.Text;           
    
                var query = new Dictionary<string, string>()
                {
                    ["userid"] = uid,
                    ["password"] = pass
                };
                string jsonData = JsonConvert.SerializeObject(query);
                var uri = QueryHelpers.AddQueryString("https://sheet2api.com/v1/CAdt0QU7TXCy/credentials/", query);
                var client = new HttpClient();
                var response = client.GetAsync(uri).Result;
                if (response.IsSuccessStatusCode)
                {
                   var strJSON = response.Content.ReadAsStringAsync().Result;
                   var a = strJSON.Substring(1, strJSON.Length - 2);
                    if (a != jsonData)
                    {
                        Label4.Text = "Invalid Credentials.!!";
                    }
                    else
                    {
                        Session["fromlogin"] = "Y";
                        Response.Redirect("Main.aspx");
                    }
    
                }
                else
                {
                    Label4.Text = "Invalid Credentials.!!";
                }
            }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.