Authentication
This commit is contained in:
parent
f51c2623c8
commit
29644d15c0
34
TestAppRuna.TokenGenerator/Program.cs
Normal file
34
TestAppRuna.TokenGenerator/Program.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
TestAppRuna.TokenGenerator/TestAppRuna.TokenGenerator.csproj
Normal file
12
TestAppRuna.TokenGenerator/TestAppRuna.TokenGenerator.csproj
Normal 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>
|
@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
|||||||
# Visual Studio Version 16
|
# Visual Studio Version 16
|
||||||
VisualStudioVersion = 16.0.31112.23
|
VisualStudioVersion = 16.0.31112.23
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
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
|
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
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
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}.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.ActiveCfg = Release|Any CPU
|
||||||
{15F529A1-7CA7-4379-886B-47D1DBDA0AB4}.Release|Any CPU.Build.0 = 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
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -13,6 +14,7 @@ namespace TestAppRuna.API
|
|||||||
{
|
{
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
|
[Authorize(AuthenticationSchemes = "nanaoaccounts")]
|
||||||
public class CustomerController : ControllerBase
|
public class CustomerController : ControllerBase
|
||||||
{
|
{
|
||||||
public CustomerController(ShopDbContext shopDbContext)
|
public CustomerController(ShopDbContext shopDbContext)
|
||||||
|
BIN
TestAppRuna/Shop.slite.db
Normal file
BIN
TestAppRuna/Shop.slite.db
Normal file
Binary file not shown.
@ -44,7 +44,13 @@ namespace TestAppRuna
|
|||||||
options.UseSqlite(Configuration.GetConnectionString("ShopDB"));
|
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.
|
// 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.UseRouting();
|
||||||
|
|
||||||
|
app.UseAuthentication();
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.UseEndpoints(endpoints =>
|
app.UseEndpoints(endpoints =>
|
||||||
{
|
{
|
||||||
endpoints.MapDefaultControllerRoute();
|
endpoints.MapDefaultControllerRoute();
|
||||||
endpoints.MapBlazorHub();
|
endpoints.MapBlazorHub();
|
||||||
endpoints.MapFallbackToPage("/_Host");
|
endpoints.MapFallbackToPage("/_Host");
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.4" />
|
||||||
<PackageReference Include="NSwag.AspNetCore" Version="13.10.8" />
|
<PackageReference Include="NSwag.AspNetCore" Version="13.10.8" />
|
||||||
<PackageReference Include="Serilog.AspNetCore" Version="4.0.0" />
|
<PackageReference Include="Serilog.AspNetCore" Version="4.0.0" />
|
||||||
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
|
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
|
||||||
|
Loading…
Reference in New Issue
Block a user