-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
174 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
samples/Kook.Net.Samples.OAuth/Kook.Net.Samples.OAuth.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<Import Project="../../Kook.Net.Sample.targets" /> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AspNet.Security.OAuth.Kook" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Kook.Net.Experimental\Kook.Net.Experimental.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System.Security.Claims; | ||
using System.Text; | ||
using AspNet.Security.OAuth.Kook; | ||
using Kook; | ||
using Kook.Rest; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
|
||
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddAuthentication(options => | ||
{ | ||
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; | ||
}) | ||
.AddCookie(options => | ||
{ | ||
options.LoginPath = "/signin"; | ||
options.LogoutPath = "/signout"; | ||
}) | ||
.AddKook(options => | ||
{ | ||
options.ClientId = string.Empty; | ||
options.ClientSecret = string.Empty; | ||
// get_user_info is added by default | ||
options.Scope.Add("get_user_guilds"); | ||
options.Events.OnCreatingTicket = async context => | ||
{ | ||
if (context.AccessToken is not { } accessToken) | ||
return; | ||
KookRestClient kookRestClient = new(); | ||
await kookRestClient.LoginAsync(TokenType.Bearer, accessToken); | ||
RestSelfUser? currentUser = kookRestClient.CurrentUser; | ||
IReadOnlyCollection<RestGuild> guilds = await kookRestClient.GetGuildsAsync(); | ||
IReadOnlyCollection<RestGuild> adminGuilds = await kookRestClient.GetAdminGuildsAsync(); | ||
}; | ||
}); | ||
WebApplication app = builder.Build(); | ||
|
||
app.UseHttpsRedirection(); | ||
app.UseAuthentication(); | ||
app.MapGet("/signin", | ||
() => Results.Challenge(new AuthenticationProperties { RedirectUri = "/" }, | ||
[KookAuthenticationDefaults.AuthenticationScheme])); | ||
app.MapGet("/signout", | ||
() => Results.SignOut(new AuthenticationProperties { RedirectUri = "/" }, | ||
[CookieAuthenticationDefaults.AuthenticationScheme])); | ||
app.MapGet("/", | ||
(HttpContext context) => | ||
{ | ||
if (context.User is { Identity.IsAuthenticated: true } claimsPrincipal) | ||
{ | ||
StringBuilder contentBuilder = new(); | ||
contentBuilder.AppendLine($"Hello, {claimsPrincipal.Identity.Name}!<br />"); | ||
Dictionary<string, string> claims = claimsPrincipal.Claims.ToDictionary(x => x.Type, x => x.Value); | ||
if (claims.TryGetValue(ClaimTypes.NameIdentifier, out string? nameIdentifier)) | ||
contentBuilder.AppendLine($"Identifier: {nameIdentifier}<br />"); | ||
if (claims.TryGetValue(ClaimTypes.Name, out string? name)) | ||
contentBuilder.AppendLine($"Name: {name}<br />"); | ||
if (claims.TryGetValue(ClaimTypes.MobilePhone, out string? mobilePhone)) | ||
contentBuilder.AppendLine($"Mobile phone: {mobilePhone}<br />"); | ||
if (claims.TryGetValue(KookAuthenticationConstants.Claims.IdentifyNumber, out string? identifyNumber)) | ||
contentBuilder.AppendLine($"Identify number: {identifyNumber}<br />"); | ||
if (claims.TryGetValue(KookAuthenticationConstants.Claims.OperatingSystem, out string? operatingSystem)) | ||
contentBuilder.AppendLine($"Operating system: {operatingSystem}<br />"); | ||
if (claims.TryGetValue(KookAuthenticationConstants.Claims.AvatarUrl, out string? avatarUrl)) | ||
contentBuilder.AppendLine($"Avatar URL: {avatarUrl}<br />"); | ||
if (claims.TryGetValue(KookAuthenticationConstants.Claims.BannerUrl, out string? bannerUrl)) | ||
contentBuilder.AppendLine($"Banner URL: {bannerUrl}<br />"); | ||
if (claims.TryGetValue(KookAuthenticationConstants.Claims.IsMobileVerified, out string? isMobileVerified)) | ||
contentBuilder.AppendLine($"Mobile verified: {isMobileVerified}<br />"); | ||
contentBuilder.AppendLine("<a href=\"/signout\">Sign out</a>"); | ||
return Results.Content(contentBuilder.ToString(), "text/html", Encoding.Default); | ||
} | ||
|
||
return Results.Content("<a href=\"/signin\">Sign in</a>", "text/html"); | ||
}); | ||
|
||
await app.RunAsync(); |
31 changes: 31 additions & 0 deletions
31
samples/Kook.Net.Samples.OAuth/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:5043", | ||
"sslPort": 5044 | ||
} | ||
}, | ||
"profiles": { | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "https://kook.natfrp.gehongyan.cn/", | ||
"applicationUrl": "https://localhost:5044;http://localhost:5043", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters