From 6643b00981aa94882b3c5b573557decb1dddcffb Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 24 Oct 2024 11:49:21 +0500 Subject: [PATCH] Add project files. --- AuthentificationExample.sln | 25 +++++++++++++++ ConsoleApp1/AuthentificationExample.csproj | 14 ++++++++ ConsoleApp1/JwtToken.cs | 8 +++++ ConsoleApp1/Program.cs | 37 ++++++++++++++++++++++ ConsoleApp1/appsettings.json | 13 ++++++++ 5 files changed, 97 insertions(+) create mode 100644 AuthentificationExample.sln create mode 100644 ConsoleApp1/AuthentificationExample.csproj create mode 100644 ConsoleApp1/JwtToken.cs create mode 100644 ConsoleApp1/Program.cs create mode 100644 ConsoleApp1/appsettings.json diff --git a/AuthentificationExample.sln b/AuthentificationExample.sln new file mode 100644 index 0000000..c441cdf --- /dev/null +++ b/AuthentificationExample.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34714.143 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AuthentificationExample", "ConsoleApp1\AuthentificationExample.csproj", "{CAB00FAC-2646-4ACF-B82D-7B5CD25D2D0A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CAB00FAC-2646-4ACF-B82D-7B5CD25D2D0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CAB00FAC-2646-4ACF-B82D-7B5CD25D2D0A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CAB00FAC-2646-4ACF-B82D-7B5CD25D2D0A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CAB00FAC-2646-4ACF-B82D-7B5CD25D2D0A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8E3A9267-5264-4EB3-8B2F-8D5E5D2451C0} + EndGlobalSection +EndGlobal diff --git a/ConsoleApp1/AuthentificationExample.csproj b/ConsoleApp1/AuthentificationExample.csproj new file mode 100644 index 0000000..c5baf53 --- /dev/null +++ b/ConsoleApp1/AuthentificationExample.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/ConsoleApp1/JwtToken.cs b/ConsoleApp1/JwtToken.cs new file mode 100644 index 0000000..97546ef --- /dev/null +++ b/ConsoleApp1/JwtToken.cs @@ -0,0 +1,8 @@ +// See https://aka.ms/new-console-template for more information +using System.Text.Json.Serialization; + +internal class JwtToken +{ + [JsonPropertyName("access_token")] + public string AccessToken { get; set; } +} \ No newline at end of file diff --git a/ConsoleApp1/Program.cs b/ConsoleApp1/Program.cs new file mode 100644 index 0000000..75114d1 --- /dev/null +++ b/ConsoleApp1/Program.cs @@ -0,0 +1,37 @@ +// See https://aka.ms/new-console-template for more information +using RestSharp; +using System.Text.Json; + +var keyCloackHost = "localhost:8080"; +var webApiHost = "localhost:44358"; + +var restClient = new RestClient(); +var request = new RestRequest($"http://{keyCloackHost}/realms/TestRealm/protocol/openid-connect/token", Method.Post); +//параметры для body +request.AddParameter("username", "myuser"); +request.AddParameter("password", 123456); +request.AddParameter("client_id", "public-client"); +request.AddParameter("grant_type", "password"); + +var keyCloackResponse = await restClient.PostAsync(request); +if (keyCloackResponse.IsSuccessful) +{ + var token = JsonSerializer.Deserialize(keyCloackResponse.Content); + var access_token = token.AccessToken; + + var webApiRequest = new RestRequest($"https://{webApiHost}/WeatherForecast", Method. Get); + webApiRequest.AddHeader("Authorization", "Bearer " + access_token); + + var webApiResponse = await restClient.GetAsync(webApiRequest); + if (webApiResponse.IsSuccessful) { + Console.WriteLine(webApiResponse.Content); + } + else + { + Console.WriteLine(webApiResponse.StatusDescription); + } +} +else +{ + Console.WriteLine(keyCloackResponse.StatusDescription); +} diff --git a/ConsoleApp1/appsettings.json b/ConsoleApp1/appsettings.json new file mode 100644 index 0000000..e3e9e31 --- /dev/null +++ b/ConsoleApp1/appsettings.json @@ -0,0 +1,13 @@ +{ + "KeyClockSettings": { + "BaseUri": "http://localhost:8080" + } + "exclude": [ + "**/bin", + "**/bower_components", + "**/jspm_packages", + "**/node_modules", + "**/obj", + "**/platforms" + ] +} \ No newline at end of file