diff --git a/TestTokenCreator/Controllers/TokenController.cs b/TestTokenCreator/Controllers/TokenController.cs index 5170996..339acdb 100644 --- a/TestTokenCreator/Controllers/TokenController.cs +++ b/TestTokenCreator/Controllers/TokenController.cs @@ -8,7 +8,6 @@ namespace TestTokenCreator.Controllers public class TokenController : ControllerBase { private static readonly HttpClient client = new HttpClient(); - private DataModel _applicationModel; public TokenController() { @@ -17,13 +16,13 @@ namespace TestTokenCreator.Controllers [HttpPost(Name = "SetApplicationModel")] public void SetApplicationModel(DataModel dataModel) { - _applicationModel = dataModel; + DataModel.Instance.SetModel(dataModel); } [HttpGet(Name = "GetApplicationModel")] public DataModel GetApplicationModel() { - return this._applicationModel; + return DataModel.Instance; } [HttpGet(Name = "GetAutorizationUrl")] @@ -31,8 +30,8 @@ namespace TestTokenCreator.Controllers { string state = "User1"; string scope = Uri.EscapeUriString("vso.code_full vso.tokens vso.tokenadministration"); - string encodedRedirectUri = Uri.EscapeUriString(_applicationModel.RedirectUri); - string urlAutorisation = $"https://app.vssps.visualstudio.com/oauth2/authorize?client_id={_applicationModel.ApplicationId}&response_type=Assertion&state={state}&scope={scope}&redirect_uri={encodedRedirectUri}"; + string encodedRedirectUri = Uri.EscapeUriString(DataModel.Instance.RedirectUri); + string urlAutorisation = $"https://app.vssps.visualstudio.com/oauth2/authorize?client_id={DataModel.Instance.ApplicationId}&response_type=Assertion&state={state}&scope={scope}&redirect_uri={encodedRedirectUri}"; return urlAutorisation; } @@ -46,7 +45,7 @@ namespace TestTokenCreator.Controllers Console.WriteLine($"Code: {code}"); Console.WriteLine($"State: {state}"); - HttpContent content = GenerateRequestPostData(_applicationModel.Secret, code, _applicationModel.RedirectUri); + HttpContent content = GenerateRequestPostData(DataModel.Instance.Secret, code, DataModel.Instance.RedirectUri); HttpResponseMessage response = await client.PostAsync("https://app.vssps.visualstudio.com/oauth2/token", content); string resp = await response.Content.ReadAsStringAsync(); return resp; diff --git a/TestTokenCreator/Controllers/WeatherForecastController.cs b/TestTokenCreator/Controllers/WeatherForecastController.cs deleted file mode 100644 index 8767079..0000000 --- a/TestTokenCreator/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace TestTokenCreator.Controllers -{ - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase - { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateTime.Now.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } - } -} \ No newline at end of file diff --git a/TestTokenCreator/Models/DataModel.cs b/TestTokenCreator/Models/DataModel.cs index 0d79267..7d18701 100644 --- a/TestTokenCreator/Models/DataModel.cs +++ b/TestTokenCreator/Models/DataModel.cs @@ -6,18 +6,22 @@ public string ApplicationId { get; set; } public string RedirectUri { get; set; } + private static readonly Lazy lazy = new Lazy(() => new DataModel()); + + public static DataModel Instance => lazy.Value; + public void SetModel(string secret, string applicationId, string redirectUri) { - Secret = secret; - ApplicationId = applicationId; - RedirectUri = redirectUri; + Instance.Secret = secret; + Instance.ApplicationId = applicationId; + Instance.RedirectUri = redirectUri; } public void SetModel(DataModel model) { - Secret = model.Secret; - ApplicationId = model.ApplicationId; - RedirectUri = model.RedirectUri; + Instance.Secret = model.Secret; + Instance.ApplicationId = model.ApplicationId; + Instance.RedirectUri = model.RedirectUri; } } } diff --git a/TestTokenCreator/Program.cs b/TestTokenCreator/Program.cs index db65cb6..290d20e 100644 --- a/TestTokenCreator/Program.cs +++ b/TestTokenCreator/Program.cs @@ -8,6 +8,7 @@ builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddSingleton(); var app = builder.Build(); diff --git a/TestTokenCreator/WeatherForecast.cs b/TestTokenCreator/WeatherForecast.cs deleted file mode 100644 index c08bd1e..0000000 --- a/TestTokenCreator/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace TestTokenCreator -{ - public class WeatherForecast - { - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -} \ No newline at end of file