Skip to content

Commit

Permalink
feat: Support AddCasdoorWebApi method (#59)
Browse files Browse the repository at this point in the history
Signed-off-by: Tan <2912363476@qq.com>
  • Loading branch information
Tanyuu authored Aug 5, 2023
1 parent 29c1450 commit 9982e7f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/Casdoor.AspNetCore/Casdoor.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,22 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.9" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.0-preview.2.22153.2" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.20" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.3" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.17" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.15" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.32" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="3.1.23" />
</ItemGroup>

Expand Down
24 changes: 19 additions & 5 deletions src/Casdoor.AspNetCore/ServiceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Casdoor.AspNetCore.Authentication
{
Expand Down Expand Up @@ -49,9 +50,10 @@ public static AuthenticationBuilder AddCasdoor(this AuthenticationBuilder builde
public static AuthenticationBuilder AddCasdoor(this AuthenticationBuilder builder, string applicationType, Action<CasdoorOptions> optionAction)
{
builder.Services.AddCasdoorClient(optionAction);
return applicationType switch {
CasdoorDefaults.WebAppApplicationType => builder.AddCasdoorWebApp(o => {}),
CasdoorDefaults.WebApiApplicationType => builder.AddCasdoorWebApi(o => {}),
return applicationType switch
{
CasdoorDefaults.WebAppApplicationType => builder.AddCasdoorWebApp(o => { }),
CasdoorDefaults.WebApiApplicationType => builder.AddCasdoorWebApi(o => { }),
_ => throw new ArgumentOutOfRangeException(nameof(applicationType))
};
}
Expand Down Expand Up @@ -85,9 +87,21 @@ public static AuthenticationBuilder AddCasdoorWebApp(this AuthenticationBuilder
return builder;
}

public static AuthenticationBuilder AddCasdoorWebApi(this AuthenticationBuilder builder, Action<OpenIdConnectOptions> openIdOptionAction)
public static AuthenticationBuilder AddCasdoorWebApi(this AuthenticationBuilder builder, Action<JwtBearerOptions> jwtBearerOptionAction)
{
throw new NotImplementedException("WebApi is not supported yet.");
builder.Services.AddAuthentication(options =>
{
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
});
builder.Services.AddOptions<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme).Configure<CasdoorOptions>(
(options, casdoorOptions) =>
{
options.Authority = casdoorOptions.Protocols.Authority;
options.Audience = casdoorOptions.Protocols.Audience;
}
);
builder.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, JwtBearerDefaults.AuthenticationScheme, jwtBearerOptionAction);
return builder;
}
}
}

0 comments on commit 9982e7f

Please sign in to comment.