You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
namespace AuthStudy.Authentication.Browser
{
public static class BrowserAuthenticationExtensions
{
public static IServiceCollection AddBrowserAuthentication
(
this IServiceCollection builder,
string AuthenticationSchemeName,
string AuthenticationDispalyName,
BrowserAuthenticationOptions Option
)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.AddService(Option);
builder.AddAuthentication(options =>
{
options.DefaultScheme = AuthenticationSchemeName;
options.DefaultAuthenticateScheme = AuthenticationSchemeName;
options.DefaultChallengeScheme = AuthenticationSchemeName;
options.DefaultForbidScheme = AuthenticationSchemeName;
options.DefaultSignInScheme = AuthenticationSchemeName;
options.DefaultSignOutScheme = AuthenticationSchemeName;
options.AddScheme<BrowserAuthenticationHandler>(AuthenticationSchemeName, AuthenticationDispalyName);
});
return builder;
}
private static IServiceCollection AddService(this IServiceCollection builder, BrowserAuthenticationOptions option)
{
BrowserAuthenticationOptions defaultOption = option ?? new();
builder.AddSingleton(defaultOption);
builder.AddSingleton<BrowserAuthenticationHandler>();
return builder;
}
}
}