Add project files.

This commit is contained in:
Olga Nemt 2024-10-24 11:49:21 +05:00
parent 8fd6a84520
commit 6643b00981
5 changed files with 97 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RestSharp" Version="112.1.0" />
</ItemGroup>
</Project>

8
ConsoleApp1/JwtToken.cs Normal file
View File

@ -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; }
}

37
ConsoleApp1/Program.cs Normal file
View File

@ -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<JwtToken>(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);
}

View File

@ -0,0 +1,13 @@
{
"KeyClockSettings": {
"BaseUri": "http://localhost:8080"
}
"exclude": [
"**/bin",
"**/bower_components",
"**/jspm_packages",
"**/node_modules",
"**/obj",
"**/platforms"
]
}