<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: OAuth with Blazor App in ArcGIS REST APIs and Services Questions</title>
    <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/oauth-with-blazor-app/m-p/1202135#M4241</link>
    <description>&lt;P&gt;In the end is pretty straight forward.&amp;nbsp; The simplest way is to simply get a token directly from a call to the /authorize method.&amp;nbsp; This was done by navigating to the authorize Url in the OnInitialized method of the Index.razor page&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;protected override void OnInitialized()
{
	try
	{
		string authorizeUrl = "https://www.arcgis.com/sharing/rest/oauth2/authorize";
		string clientId = Configuration["Authorization:clientId"]!.ToString();
		string redirectUrl = Configuration["Authorization:redirectUrl"]!.ToString();
		string responseType = "token";

		UriBuilder builder = new UriBuilder(authorizeUrl)
		{
			Query = $"client_id={clientId}&amp;amp;redirect_uri={redirectUrl}&amp;amp;response_type={responseType}"
		};

		string oAuthUrl = builder.ToString();
		NavigationManager.NavigateTo(oAuthUrl);
	}
	catch (Exception e)
	{
		Console.WriteLine(e);
	}
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then in the page defined in the redirectUrl you grab the token from the return returned url&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;protected override void OnInitialized()
{
	try
	{
		_token =  Navigation.Uri.Substring(Navigation.Uri.IndexOf("=", StringComparison.Ordinal) + 1);
	}
	catch (Exception e)
	{
		Console.WriteLine(e);
	}
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;An approach which I think is a bit more secure would involve requesting code from /authorize method and then getting the token in the redirect page using the /token method.&amp;nbsp; In this case in the Index.razor we have&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;protected override void OnInitialized()
{
	try
	{
		string authorizeUrl = "https://www.arcgis.com/sharing/rest/oauth2/authorize";
		string clientId = Configuration["Authorization:clientId"]!.ToString();
		string redirectUrl = Configuration["Authorization:redirectUrl"]!.ToString();
		string responseType = "code";
		string codeChallenge = CreateChallangeCode();
		string codeChallengeMethod = "plain";

		UriBuilder builder = new UriBuilder(authorizeUrl)
		{
			Query = $"client_id={clientId}&amp;amp;redirect_uri={redirectUrl}&amp;amp;response_type={responseType}&amp;amp;code_challenge={codeChallenge}&amp;amp;code_challenge_method={codeChallengeMethod}"
		};

		string oAuthUrl = builder.ToString();
		NavigationManager.NavigateTo(oAuthUrl);
	}
	catch (Exception e)
	{
		Console.WriteLine(e);
	}
}

private string CreateChallangeCode()
{
	using SHA256 sha256 = SHA256.Create();
	byte[] data = sha256.ComputeHash(Encoding.UTF8.GetBytes("blazor"));

	var builder = new StringBuilder();

	// Loop through each byte of the hashed data
	// and format each one as a hexadecimal string.
	foreach (var b in data)
	{
		builder.Append(b.ToString("x2"));
	}


	return builder.ToString();
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and in the redirect page get the token from the returned code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;protected override async void OnInitialized()
{
	try
	{
		string code =  Navigation.Uri.Substring(Navigation.Uri.IndexOf("=", StringComparison.Ordinal) + 1);
		await RequestToken(code);
	}
	catch (Exception e)
	{
		Console.WriteLine(e);
	}
}

private async Task RequestToken(string code)
{
	string tokenUrl = "https://www.arcgis.com/sharing/rest/oauth2/token";
	string clientId = Configuration["Authorization:clientId"]!;
	string redirectUrl = Configuration["Authorization:redirectUrl"]!;
	string codeChallenge = CreateCodeChallenge();

	var dictionary = new Dictionary&amp;lt;string, string&amp;gt;
	{
		{ "client_id", clientId },
		{ "grant_type", "authorization_code" },
		{ "code", code! },
		{ "code_verifier", codeChallenge },
		{ "redirect_uri", redirectUrl }
	};

	FormUrlEncodedContent content = new FormUrlEncodedContent(dictionary);


	using HttpClient client = new HttpClient();
	var response = await client.PostAsync(tokenUrl, content);
	var json = await response.Content.ReadAsStringAsync();

	JsonNode node = JsonNode.Parse(json)!;

	_token = node!["access_token"]!.ToString();
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 11 Aug 2022 19:26:10 GMT</pubDate>
    <dc:creator>JoeHershman</dc:creator>
    <dc:date>2022-08-11T19:26:10Z</dc:date>
    <item>
      <title>OAuth with Blazor App</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/oauth-with-blazor-app/m-p/1196905#M4223</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am wondering if anyone has tried (successfully) to setup OAuth in a Blazor Web Assembly.&amp;nbsp; I think I have things configured based on the documentation but continue to get an error saying it is not setup correct.&amp;nbsp; Without any specifics.&lt;/P&gt;&lt;P&gt;Thanks - joe&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 17:28:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/oauth-with-blazor-app/m-p/1196905#M4223</guid>
      <dc:creator>JoeHershman</dc:creator>
      <dc:date>2022-07-28T17:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth with Blazor App</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/oauth-with-blazor-app/m-p/1197251#M4225</link>
      <description>&lt;P&gt;I haven't&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/428228"&gt;@joe&lt;/a&gt;&amp;nbsp;but if you solve it let us know how!!&lt;/P&gt;&lt;P&gt;Thanks and good luck!&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jul 2022 08:14:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/oauth-with-blazor-app/m-p/1197251#M4225</guid>
      <dc:creator>Raul_Jimenez</dc:creator>
      <dc:date>2022-07-29T08:14:51Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth with Blazor App</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/oauth-with-blazor-app/m-p/1202135#M4241</link>
      <description>&lt;P&gt;In the end is pretty straight forward.&amp;nbsp; The simplest way is to simply get a token directly from a call to the /authorize method.&amp;nbsp; This was done by navigating to the authorize Url in the OnInitialized method of the Index.razor page&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;protected override void OnInitialized()
{
	try
	{
		string authorizeUrl = "https://www.arcgis.com/sharing/rest/oauth2/authorize";
		string clientId = Configuration["Authorization:clientId"]!.ToString();
		string redirectUrl = Configuration["Authorization:redirectUrl"]!.ToString();
		string responseType = "token";

		UriBuilder builder = new UriBuilder(authorizeUrl)
		{
			Query = $"client_id={clientId}&amp;amp;redirect_uri={redirectUrl}&amp;amp;response_type={responseType}"
		};

		string oAuthUrl = builder.ToString();
		NavigationManager.NavigateTo(oAuthUrl);
	}
	catch (Exception e)
	{
		Console.WriteLine(e);
	}
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then in the page defined in the redirectUrl you grab the token from the return returned url&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;protected override void OnInitialized()
{
	try
	{
		_token =  Navigation.Uri.Substring(Navigation.Uri.IndexOf("=", StringComparison.Ordinal) + 1);
	}
	catch (Exception e)
	{
		Console.WriteLine(e);
	}
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;An approach which I think is a bit more secure would involve requesting code from /authorize method and then getting the token in the redirect page using the /token method.&amp;nbsp; In this case in the Index.razor we have&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;protected override void OnInitialized()
{
	try
	{
		string authorizeUrl = "https://www.arcgis.com/sharing/rest/oauth2/authorize";
		string clientId = Configuration["Authorization:clientId"]!.ToString();
		string redirectUrl = Configuration["Authorization:redirectUrl"]!.ToString();
		string responseType = "code";
		string codeChallenge = CreateChallangeCode();
		string codeChallengeMethod = "plain";

		UriBuilder builder = new UriBuilder(authorizeUrl)
		{
			Query = $"client_id={clientId}&amp;amp;redirect_uri={redirectUrl}&amp;amp;response_type={responseType}&amp;amp;code_challenge={codeChallenge}&amp;amp;code_challenge_method={codeChallengeMethod}"
		};

		string oAuthUrl = builder.ToString();
		NavigationManager.NavigateTo(oAuthUrl);
	}
	catch (Exception e)
	{
		Console.WriteLine(e);
	}
}

private string CreateChallangeCode()
{
	using SHA256 sha256 = SHA256.Create();
	byte[] data = sha256.ComputeHash(Encoding.UTF8.GetBytes("blazor"));

	var builder = new StringBuilder();

	// Loop through each byte of the hashed data
	// and format each one as a hexadecimal string.
	foreach (var b in data)
	{
		builder.Append(b.ToString("x2"));
	}


	return builder.ToString();
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and in the redirect page get the token from the returned code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;protected override async void OnInitialized()
{
	try
	{
		string code =  Navigation.Uri.Substring(Navigation.Uri.IndexOf("=", StringComparison.Ordinal) + 1);
		await RequestToken(code);
	}
	catch (Exception e)
	{
		Console.WriteLine(e);
	}
}

private async Task RequestToken(string code)
{
	string tokenUrl = "https://www.arcgis.com/sharing/rest/oauth2/token";
	string clientId = Configuration["Authorization:clientId"]!;
	string redirectUrl = Configuration["Authorization:redirectUrl"]!;
	string codeChallenge = CreateCodeChallenge();

	var dictionary = new Dictionary&amp;lt;string, string&amp;gt;
	{
		{ "client_id", clientId },
		{ "grant_type", "authorization_code" },
		{ "code", code! },
		{ "code_verifier", codeChallenge },
		{ "redirect_uri", redirectUrl }
	};

	FormUrlEncodedContent content = new FormUrlEncodedContent(dictionary);


	using HttpClient client = new HttpClient();
	var response = await client.PostAsync(tokenUrl, content);
	var json = await response.Content.ReadAsStringAsync();

	JsonNode node = JsonNode.Parse(json)!;

	_token = node!["access_token"]!.ToString();
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2022 19:26:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/oauth-with-blazor-app/m-p/1202135#M4241</guid>
      <dc:creator>JoeHershman</dc:creator>
      <dc:date>2022-08-11T19:26:10Z</dc:date>
    </item>
    <item>
      <title>Re: OAuth with Blazor App</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/oauth-with-blazor-app/m-p/1202554#M4244</link>
      <description>&lt;P&gt;Awesome&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/428228"&gt;@joe&lt;/a&gt;&amp;nbsp;!! thanks for taking the time to share&amp;nbsp;the solution &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Aug 2022 20:36:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/oauth-with-blazor-app/m-p/1202554#M4244</guid>
      <dc:creator>Raul_Jimenez</dc:creator>
      <dc:date>2022-08-12T20:36:25Z</dc:date>
    </item>
  </channel>
</rss>

