zoukankan      html  css  js  c++  java
  • 3.0AuthenticationBuilder&CookieExtensions&JwtBearerExtensions【->IServiceCollection 】

    using System;
    using System.Diagnostics.CodeAnalysis;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.DependencyInjection.Extensions;
    using Microsoft.Extensions.Options;
    
    namespace Microsoft.AspNetCore.Authentication
    {
        /// <summary>
        /// Used to configure authentication
        /// </summary>
        public class AuthenticationBuilder
        {
            /// <summary>
            /// Initializes a new instance of <see cref="AuthenticationBuilder"/>.
            /// </summary>
            /// <param name="services">The services being configured.</param>
            public AuthenticationBuilder(IServiceCollection services)
                => Services = services;
    
            /// <summary>
            /// The services being configured.
            /// </summary>
            public virtual IServiceCollection Services { get; }
    
            private AuthenticationBuilder AddSchemeHelper<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
                where TOptions : AuthenticationSchemeOptions, new()
                where THandler : class, IAuthenticationHandler
            {
                Services.Configure<AuthenticationOptions>(o =>
                {
                    o.AddScheme(authenticationScheme, scheme => {
                        scheme.HandlerType = typeof(THandler);
                        scheme.DisplayName = displayName;
                    });
                });
                if (configureOptions != null)
                {
                    Services.Configure(authenticationScheme, configureOptions);
                }
                Services.AddOptions<TOptions>(authenticationScheme).Validate(o => {
                    o.Validate(authenticationScheme);
                    return true;
                });
                Services.AddTransient<THandler>();
                return this;
            }
    
            /// <summary>
            /// Adds a <see cref="AuthenticationScheme"/> which can be used by <see cref="IAuthenticationService"/>.
            /// </summary>
            /// <typeparam name="TOptions">The <see cref="AuthenticationSchemeOptions"/> type to configure the handler."/>.</typeparam>
            /// <typeparam name="THandler">The <see cref="AuthenticationHandler{TOptions}"/> used to handle this scheme.</typeparam>
            /// <param name="authenticationScheme">The name of this scheme.</param>
            /// <param name="displayName">The display name of this scheme.</param>
            /// <param name="configureOptions">Used to configure the scheme options.</param>
            /// <returns>The builder.</returns>
            public virtual AuthenticationBuilder AddScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
                where TOptions : AuthenticationSchemeOptions, new()
                where THandler : AuthenticationHandler<TOptions>
                => AddSchemeHelper<TOptions, THandler>(authenticationScheme, displayName, configureOptions);
    
            /// <summary>
            /// Adds a <see cref="AuthenticationScheme"/> which can be used by <see cref="IAuthenticationService"/>.
            /// </summary>
            /// <typeparam name="TOptions">The <see cref="AuthenticationSchemeOptions"/> type to configure the handler."/>.</typeparam>
            /// <typeparam name="THandler">The <see cref="AuthenticationHandler{TOptions}"/> used to handle this scheme.</typeparam>
            /// <param name="authenticationScheme">The name of this scheme.</param>
            /// <param name="configureOptions">Used to configure the scheme options.</param>
            /// <returns>The builder.</returns>
            public virtual AuthenticationBuilder AddScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string authenticationScheme, Action<TOptions>? configureOptions)
                where TOptions : AuthenticationSchemeOptions, new()
                where THandler : AuthenticationHandler<TOptions>
                => AddScheme<TOptions, THandler>(authenticationScheme, displayName: null, configureOptions: configureOptions);
    
            /// <summary>
            /// Adds a <see cref="RemoteAuthenticationHandler{TOptions}"/> based <see cref="AuthenticationScheme"/> that supports remote authentication
            /// which can be used by <see cref="IAuthenticationService"/>.
            /// </summary>
            /// <typeparam name="TOptions">The <see cref="RemoteAuthenticationOptions"/> type to configure the handler."/>.</typeparam>
            /// <typeparam name="THandler">The <see cref="RemoteAuthenticationHandler{TOptions}"/> used to handle this scheme.</typeparam>
            /// <param name="authenticationScheme">The name of this scheme.</param>
            /// <param name="displayName">The display name of this scheme.</param>
            /// <param name="configureOptions">Used to configure the scheme options.</param>
            /// <returns>The builder.</returns>
            public virtual AuthenticationBuilder AddRemoteScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
                where TOptions : RemoteAuthenticationOptions, new()
                where THandler : RemoteAuthenticationHandler<TOptions>
            {
                Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<TOptions>, EnsureSignInScheme<TOptions>>());
                return AddScheme<TOptions, THandler>(authenticationScheme, displayName, configureOptions: configureOptions);
            }
    
            /// <summary>
            /// Adds a <see cref="PolicySchemeHandler"/> based authentication handler which can be used to
            /// redirect to other authentication schemes.
            /// </summary>
            /// <param name="authenticationScheme">The name of this scheme.</param>
            /// <param name="displayName">The display name of this scheme.</param>
            /// <param name="configureOptions">Used to configure the scheme options.</param>
            /// <returns>The builder.</returns>
            public virtual AuthenticationBuilder AddPolicyScheme(string authenticationScheme, string? displayName, Action<PolicySchemeOptions> configureOptions)
                => AddSchemeHelper<PolicySchemeOptions, PolicySchemeHandler>(authenticationScheme, displayName, configureOptions);
    
            // Used to ensure that there's always a default sign in scheme that's not itself
            private class EnsureSignInScheme<TOptions> : IPostConfigureOptions<TOptions> where TOptions : RemoteAuthenticationOptions
            {
                private readonly AuthenticationOptions _authOptions;
    
                public EnsureSignInScheme(IOptions<AuthenticationOptions> authOptions)
                {
                    _authOptions = authOptions.Value;
                }
    
                public void PostConfigure(string name, TOptions options)
                {
                    options.SignInScheme ??= _authOptions.DefaultSignInScheme ?? _authOptions.DefaultScheme;
                }
            }
        }
    }
    using System;
    using Microsoft.AspNetCore.Authentication;
    using Microsoft.AspNetCore.Authentication.Cookies;
    using Microsoft.Extensions.DependencyInjection.Extensions;
    using Microsoft.Extensions.Options;
    
    namespace Microsoft.Extensions.DependencyInjection
    {
        /// <summary>
        /// Extension methods to configure cookie authentication.
        /// </summary>
        public static class CookieExtensions
        {
            /// <summary>
            /// Adds cookie authentication to <see cref="AuthenticationBuilder"/> using the default scheme.
            /// The default scheme is specified by <see cref="CookieAuthenticationDefaults.AuthenticationScheme"/>.
            /// <para>
            /// Cookie authentication uses a HTTP cookie persisted in the client to perform authentication.
            /// </para>
            /// </summary>
            /// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
            /// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
            public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder)
                => builder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme);
    
            /// <summary>
            /// Adds cookie authentication to <see cref="AuthenticationBuilder"/> using the specified scheme.
            /// <para>
            /// Cookie authentication uses a HTTP cookie persisted in the client to perform authentication.
            /// </para>
            /// </summary>
            /// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
            /// <param name="authenticationScheme">The authentication scheme.</param>
            /// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
            public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme)
                => builder.AddCookie(authenticationScheme, configureOptions: null!);
    
            /// <summary>
            /// Adds cookie authentication to <see cref="AuthenticationBuilder"/> using the default scheme.
            /// The default scheme is specified by <see cref="CookieAuthenticationDefaults.AuthenticationScheme"/>.
            /// <para>
            /// Cookie authentication uses a HTTP cookie persisted in the client to perform authentication.
            /// </para>
            /// </summary>
            /// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
            /// <param name="configureOptions">A delegate to configure <see cref="CookieAuthenticationOptions"/>.</param>
            /// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
            public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, Action<CookieAuthenticationOptions> configureOptions)
                => builder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, configureOptions);
    
            /// <summary>
            /// Adds cookie authentication to <see cref="AuthenticationBuilder"/> using the specified scheme.
            /// <para>
            /// Cookie authentication uses a HTTP cookie persisted in the client to perform authentication.
            /// </para>
            /// </summary>
            /// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
            /// <param name="authenticationScheme">The authentication scheme.</param>
            /// <param name="configureOptions">A delegate to configure <see cref="CookieAuthenticationOptions"/>.</param>
            /// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
            public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, Action<CookieAuthenticationOptions> configureOptions)
                => builder.AddCookie(authenticationScheme, displayName: null, configureOptions: configureOptions);
    
            /// <summary>
            /// Adds cookie authentication to <see cref="AuthenticationBuilder"/> using the specified scheme.
            /// <para>
            /// Cookie authentication uses a HTTP cookie persisted in the client to perform authentication.
            /// </para>
            /// </summary>
            /// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
            /// <param name="authenticationScheme">The authentication scheme.</param>
            /// <param name="displayName">A display name for the authentication handler.</param>
            /// <param name="configureOptions">A delegate to configure <see cref="CookieAuthenticationOptions"/>.</param>
            /// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
            public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, string? displayName, Action<CookieAuthenticationOptions> configureOptions)
            {
                builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<CookieAuthenticationOptions>, PostConfigureCookieAuthenticationOptions>());
                builder.Services.AddOptions<CookieAuthenticationOptions>(authenticationScheme).Validate(o => o.Cookie.Expiration == null, "Cookie.Expiration is ignored, use ExpireTimeSpan instead.");
                return builder.AddScheme<CookieAuthenticationOptions, CookieAuthenticationHandler>(authenticationScheme, displayName, configureOptions);
            }
        }
    }
    using System;
    using Microsoft.AspNetCore.Authentication;
    using Microsoft.AspNetCore.Authentication.JwtBearer;
    using Microsoft.Extensions.DependencyInjection.Extensions;
    using Microsoft.Extensions.Options;
    
    namespace Microsoft.Extensions.DependencyInjection
    {
        /// <summary>
        /// Extension methods to configure JWT bearer authentication.
        /// </summary>
        public static class JwtBearerExtensions
        {
            /// <summary>
            /// Enables JWT-bearer authentication using the default scheme <see cref="JwtBearerDefaults.AuthenticationScheme"/>.
            /// <para>
            /// JWT bearer authentication performs authentication by extracting and validating a JWT token from the <c>Authorization</c> request header.
            /// </para>
            /// </summary>
            /// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
            /// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
            public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder)
                => builder.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, _ => { });
    
            /// <summary>
            /// Enables JWT-bearer authentication using the default scheme <see cref="JwtBearerDefaults.AuthenticationScheme"/>.
            /// <para>
            /// JWT bearer authentication performs authentication by extracting and validating a JWT token from the <c>Authorization</c> request header.
            /// </para>
            /// </summary>
            /// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
            /// <param name="configureOptions">A delegate that allows configuring <see cref="JwtBearerOptions"/>.</param>
            /// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
            public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, Action<JwtBearerOptions> configureOptions)
                => builder.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, configureOptions);
    
            /// <summary>
            /// Enables JWT-bearer authentication using the specified scheme.
            /// <para>
            /// JWT bearer authentication performs authentication by extracting and validating a JWT token from the <c>Authorization</c> request header.
            /// </para>
            /// </summary>
            /// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
            /// <param name="authenticationScheme">The authentication scheme.</param>
            /// <param name="configureOptions">A delegate that allows configuring <see cref="JwtBearerOptions"/>.</param>
            /// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
            public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, Action<JwtBearerOptions> configureOptions)
                => builder.AddJwtBearer(authenticationScheme, displayName: null, configureOptions: configureOptions);
    
            /// <summary>
            /// Enables JWT-bearer authentication using the specified scheme.
            /// <para>
            /// JWT bearer authentication performs authentication by extracting and validating a JWT token from the <c>Authorization</c> request header.
            /// </para>
            /// </summary>
            /// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
            /// <param name="authenticationScheme">The authentication scheme.</param>
            /// <param name="displayName">The display name for the authentication handler.</param>
            /// <param name="configureOptions">A delegate that allows configuring <see cref="JwtBearerOptions"/>.</param>
            /// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
            public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, string? displayName, Action<JwtBearerOptions> configureOptions)
            {
                builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<JwtBearerOptions>, JwtBearerPostConfigureOptions>());
                return builder.AddScheme<JwtBearerOptions, JwtBearerHandler>(authenticationScheme, displayName, configureOptions);
            }
        }
    }
  • 相关阅读:
    给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1},
    输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
    定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
    用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
    输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
    给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。
    在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3
    Oracle-SQL-按月统计自助终端交易量
    Hibernate table schema 的设置与应用
    Oracle函数之chr
  • 原文地址:https://www.cnblogs.com/htlp/p/15256539.html
Copyright © 2011-2022 走看看