Need .Net C# sample project to login

@HEMANT_DAT_13838195
Hello,

Here is a sample code snippet for logging into UpStox using a C# application (utilizing the UpStox SDK). An explanation of the code is provided below.

string url = "http://localhost:11400/";
var listener = new HttpListener();
listener.Prefixes.Add(url);
listener.Start();
btnContinue.Enabled = false;
UpStoxClient.Api.LoginApi api = new UpStoxClient.Api.LoginApi();
// Open login page in default browser
ProcessStartInfo psi = new ProcessStartInfo
{
    FileName = $"https://api.upstox.com/v2/login/authorization/dialog?client_id={UrlEncoder.Default.Encode(Program.UpStoxAPIKey)}&redirect_uri=" + UrlEncoder.Default.Encode(url),
    UseShellExecute = true
};
Process.Start(psi);
var context = await listener.GetContextAsync();
string response = "UpStox Login successful!";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(response);
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.OutputStream.Close();
string query = context.Request.Url.Query;
listener.Stop();
// Parse the query string
var queryParams = HttpUtility.ParseQueryString(query);

if (queryParams.HasKeys())
{
    // Get the value of "apisession"
    string code = queryParams["code"];
    if (!string.IsNullOrEmpty(code))
    {
        TokenResponse tokenResp = api.Token(code, Program.UpStoxAPIKey, Program.UpStoxAPISecret, url, "authorization_code");
        Program.UpStoxToken = tokenResp.AccessToken;
        Configuration.Default.AccessToken = tokenResp.AccessToken;
        this.Focus();
    }
    else
    {
        btnContinue.Enabled = true;
        MessageBoxAdv.Show("UpStox login failed.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
}

  1. Setup Local HTTP Listener:

    • A local HTTP server is started using HttpListener to listen on a specific URL (e.g., http://localhost:11400/).
    • This URL acts as the redirect_uri where UpStox will send the callback after successful login.
  2. Redirect URL in UpStox API:

    • Yes, you need to register the same redirect_uri (e.g., http://localhost:11400/) in your UpStox API configuration to ensure UpStox redirects to this URL after user authentication.
  3. Open Login Page:

    • The application constructs the UpStox login URL with necessary parameters like:
      • client_id (API key from UpStox).
      • redirect_uri (the registered URL where UpStox will send the callback).
    • Opens this URL in the default browser, prompting the user to log in.
  4. Wait for Callback:

    • The HttpListener waits for the UpStox API to redirect the user to the redirect_uri after login.
    • Once redirected, the authorization code is included in the query string of the URL.
  5. Extract Authorization Code:

    • The application extracts the code parameter from the callback URL query string, which serves as the authorization code.
  6. Exchange Code for Access Token:

    • The authorization code is sent to UpStox’s API along with:
      • client_id (API key).
      • client_secret. (API Secret)
      • redirect_uri. (The registered redirect URL)
    • The API responds with an access token if the code is valid.
  7. Store the Access Token:

    • The access token is saved for use in subsequent API requests to UpStox, enabling authenticated operations.
  8. Error Handling:

    • If the code parameter is missing or invalid, an error is displayed, and the process is halted.

Hope it helps.

Thanks