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.

59 lines
2.0 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 BaseBrowserAuthenticationExtensions
{
#region 基于接口的扩展注册
public static IServiceCollection AddBaseBrowserAuthentication
(
this IServiceCollection builder,
string authenticationSchemeName,
string authenticationDisplayName,
BrowserAuthenticationOptions authenticationOption
)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.AddService(authenticationOption);
builder.AddAuthentication(options =>
{
options.DefaultScheme = authenticationSchemeName;
options.DefaultAuthenticateScheme = authenticationSchemeName;
options.DefaultChallengeScheme = authenticationSchemeName;
options.DefaultForbidScheme = authenticationSchemeName;
options.DefaultSignInScheme = authenticationSchemeName;
options.DefaultSignOutScheme = authenticationSchemeName;
options.AddScheme<BrowserAuthenticationBaseHandler>(authenticationSchemeName, authenticationDisplayName);
});
return builder;
}
private static IServiceCollection AddService(this IServiceCollection builder, BrowserAuthenticationOptions option)
{
BrowserAuthenticationOptions defaultOption = option ?? new(){AllowBrowsers = BrowserAuthenticationDefault.AllowBrowsers};
builder.AddSingleton(defaultOption);
builder.AddSingleton<BrowserAuthenticationBaseHandler>();
return builder;
}
#endregion
}
}