How to redirect to login page after executing ouath2 authorization request

1.3k Views Asked by At

good day. I’m new to C# and OAuth. I'm trying to implement OAuth2 in Blazor using RestSharp. I have the following code:

@page "/infusionsoft" @using System.Globalization

Infusionsoft

Request authentication

@code {
    @using RestSharp;
    @using RestSharp.Authenticators;
    @using Newtonsoft.Json;

public void Foo1()
{
        string url = "https://signin.infusionsoft.com/app/oauth/authorize";
        string client_id = "myid";
        string client_secret = "mysecret";

        //request token
        var restclient = new RestClient(url);
        RestRequest request = new RestRequest("request/oauth") { Method = Method.POST };
        request.AddHeader("Accept", "application/json");
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
        request.AddParameter("client_id", client_id);
       request.AddParameter("client_secret", client_secret);
        request.AddParameter("grant_type", "authorization_code");//+

        IRestResponse tResponse = restclient.Execute(request);
        Console.WriteLine(tResponse.Content);


}

}

It’s supposed to redirect my app to a sign in page but right now it doesn’t do anything. I have tried google but I still don’t get it :(. Also, how do I start calling the apis once I get the authorization. Any sample code is greatly appreciated. Thanks.

2

There are 2 best solutions below

0
ajay gandhi On BEST ANSWER

You should redirect the user to authentication server by sending HTTP status code 302 and then specifying the auth server URL and query parameters in Redirect URL.

0
li223 On

I'm not overly familiar with the things you're using, but from what I can tell, you aren't actually redirecting at all.

You're sending the POST request to the OAuth URL using RestSharp which doesn't redirect the user and instead just sends the request from your application and saves the response in tResponse.

It seems like you'll need to use IUriHelper#NavigateTo(url) to redirect to the OAuth page. You can inject it using @inject IUriHelper uriHelper, then build the URL with its parameters, and then pass it uriHelper.NavigateTo(url). Hope this helps.

Links:

Redirecting in blazor with parameter

https://medium.com/cloudnimble/redirects-in-blazor-apps-75b3f4709d57