Authentication

This commit is contained in:
Damillora 2021-03-25 11:21:55 +07:00
parent f51c2623c8
commit 29644d15c0
7 changed files with 68 additions and 5 deletions

View File

@ -0,0 +1,34 @@
using IdentityModel.Client;
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace TestAppRuna.TokenGenerator
{
class Program
{
static void Main(string[] args)
{
Startup().Wait();
}
static async Task Startup()
{
var clientID = "testappruna";
var clientSecret = "1071ccf6-1ea7-4d75-b6e7-28846e386eb5";
var client = new HttpClient();
var disco = await client.GetDiscoveryDocumentAsync("https://accounts.nanao.moe/auth/realms/staging");
var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = clientID,
ClientSecret = clientSecret,
Scope = "customer-api"
});
Console.WriteLine(response.AccessToken);
}
}
}

View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="IdentityModel" Version="5.0.1" />
</ItemGroup>
</Project>

View File

@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31112.23
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestAppRuna", "TestAppRuna\TestAppRuna.csproj", "{07486869-70E3-49C6-8C73-782BFCBD4284}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestAppRuna", "TestAppRuna\TestAppRuna.csproj", "{07486869-70E3-49C6-8C73-782BFCBD4284}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestAppRuna.Entities", "TestAppRuna.Entities\TestAppRuna.Entities.csproj", "{15F529A1-7CA7-4379-886B-47D1DBDA0AB4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestAppRuna.Entities", "TestAppRuna.Entities\TestAppRuna.Entities.csproj", "{15F529A1-7CA7-4379-886B-47D1DBDA0AB4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestAppRuna.TokenGenerator", "TestAppRuna.TokenGenerator\TestAppRuna.TokenGenerator.csproj", "{31FC0DAE-E09B-4F14-9C1F-2F5A79D76353}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -21,6 +23,10 @@ Global
{15F529A1-7CA7-4379-886B-47D1DBDA0AB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15F529A1-7CA7-4379-886B-47D1DBDA0AB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15F529A1-7CA7-4379-886B-47D1DBDA0AB4}.Release|Any CPU.Build.0 = Release|Any CPU
{31FC0DAE-E09B-4F14-9C1F-2F5A79D76353}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{31FC0DAE-E09B-4F14-9C1F-2F5A79D76353}.Debug|Any CPU.Build.0 = Debug|Any CPU
{31FC0DAE-E09B-4F14-9C1F-2F5A79D76353}.Release|Any CPU.ActiveCfg = Release|Any CPU
{31FC0DAE-E09B-4F14-9C1F-2F5A79D76353}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
@ -13,6 +14,7 @@ namespace TestAppRuna.API
{
[Route("api/[controller]")]
[ApiController]
[Authorize(AuthenticationSchemes = "nanaoaccounts")]
public class CustomerController : ControllerBase
{
public CustomerController(ShopDbContext shopDbContext)

BIN
TestAppRuna/Shop.slite.db Normal file

Binary file not shown.

View File

@ -44,7 +44,13 @@ namespace TestAppRuna
options.UseSqlite(Configuration.GetConnectionString("ShopDB"));
});
services.AddSwaggerDocument();
services.AddAuthentication()
.AddJwtBearer("nanaoaccounts", options =>
{
options.Authority = "https://accounts.nanao.moe/auth/realms/staging";
options.Audience = "testappruna";
}
);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@ -76,13 +82,15 @@ namespace TestAppRuna
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
}

View File

@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.4" />
<PackageReference Include="NSwag.AspNetCore" Version="13.10.8" />
<PackageReference Include="Serilog.AspNetCore" Version="4.0.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />