Files
SHERPA/SHERPA.OAuth/Pages/Index.cshtml.cs
T
2023-01-20 16:40:03 +01:00

47 lines
1.5 KiB
C#

using It.FattureInCloud.Sdk.OauthHelper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Collections.Generic;
namespace SHERPA.OAuth.Pages;
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
string CLIENT_ID = "Cz12xh57900cepFGMQq8yLAa7x8pcSp4";
string CLIENT_SECRET = "PL6kvy0czp6O5sScpovXo7zXwCtSZLPZGJAWpCPl9wGkFnRYrmw16r8C56aLMXQv\r\n";
string code = HttpContext.Request.Query["code"];
var oauth = new OAuth2AuthorizationCodeManager(CLIENT_ID, CLIENT_SECRET, "https://localhost:7233/index");
//var oauth = new OAuth2AuthorizationCodeManager(CLIENT_ID, CLIENT_SECRET, "https://localhost:7233/oauth");
if (code is null)
{
var scopes = new List<Scope> { Scope.ENTITY_SUPPLIERS_READ };
var url = oauth.GetAuthorizationUrl(scopes, "EXAMPLE_STATE");
Response.Redirect(url);
}
else
{
var token = oauth.FetchToken(code);
using StreamWriter file = new("token.json");
file.Write(JsonConvert.SerializeObject(token)); //saving the oAuth access token in the file token.json in the bin folder
file.Close();
ViewData["Content"] = "Token saved succesfully in token.json in your bin folder";
}
}
}