src/Hx.IdentityServer.Extensions/ServiceExtensions/IdentityServerServiceExtensions.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using Hx.IdentityServer.Authorization;
using Hx.IdentityServer.Common;
using Hx.IdentityServer.Data;
using Hx.IdentityServer.Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class IdentityServerServiceExtensions
    {
        /// <summary>
        /// 添加IdentityServer4
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static IIdentityServerBuilder AddIdentityServerSetup(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddApplicationDb(configuration);

            services.AddIdentity<ApplicationUser, ApplicationRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();
            services.ConfigureApplicationCookie(options =>
            {
                //options.Events.OnRedirectToAccessDenied = (c) =>
                //{
                //    return Task.CompletedTask;
                //};
                options.AccessDeniedPath = new Microsoft.AspNetCore.Http.PathString("/oauth2/accessdenied");
                options.LoginPath = new Microsoft.AspNetCore.Http.PathString("/oauth2/authorize");
            });

            var builder = services.AddIdentityServer(options =>
            {
                //options.Events.RaiseErrorEvents = true;
                //options.Events.RaiseInformationEvents = true;
                //options.Events.RaiseFailureEvents = true;
                //options.Events.RaiseSuccessEvents = true;
                //// see https://identityserver4.readthedocs.io/en/latest/topics/resources.html
                //options.EmitStaticAudienceClaim = true;
                options.UserInteraction.LoginUrl = "/oauth2/authorize";
                options.UserInteraction.LogoutUrl = "/oauth2/logout";
                options.UserInteraction.ErrorUrl = "/error";
            }).AddAspNetIdentity<ApplicationUser>();

            builder.AddConfigAndOperateStore(configuration);
            // not recommended for production - you need to store your key material somewhere secure
            builder.AddDeveloperSigningCredential();
            //services.AddAuthentication()
            //    .AddGoogle(options =>
            //    {
            //        options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
后面还有 17 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。