Ejemplo n.º 1
0
        public async Task <IActionResult> Register(string code, string state)
        {
            string message = "";

            if (code.IsNullOrEmpty() || state.IsNullOrEmpty())
            {
                message = "返回信息有误";
                return(RedirectToAction("Index", new { message = message }));
            }

            if (!int.TryParse(state, out int id))
            {
                message = "Id信息不正确";
                return(RedirectToAction("Index", new { message = message }));
            }

            var application = LiteDbHelper.Instance.GetDataById <ApplicationEntity>(nameof(ApplicationEntity), id);

            if (application == null)
            {
                message = "未找到对应的应用信息";
                return(RedirectToAction("Index", new { message = message }));
            }

            Graph graph  = new Graph(application.ClientId, application.ClientSecret, Request.GetRegisterUrl(), Constants.Scopes);
            var   result = await graph.GetGraphWithCode(code);

            if (result == null)
            {
                return(RedirectToAction("Index", new { message = "添加失败" }));
            }

            application.AuthorizationStatus = true;
            application.IsEnable            = true;
            LiteDbHelper.Instance.InsertOrUpdate(nameof(ApplicationEntity), application);
            SchedulerUtil.AddScheduler(application.Id);
            return(RedirectToAction("Index", new { message = "添加成功" }));
        }
Ejemplo n.º 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var liteDatabase = new LiteDatabase("Filename=db/user.db;Connection=Shared");

            LiteDbHelper.InitDb(liteDatabase);
            var applications = LiteDbHelper.Instance.GetCollection <ApplicationEntity>(nameof(ApplicationEntity)).Find(x => x.AuthorizationStatus && x.IsEnable);

            if (applications != null && applications.Any())
            {
                foreach (var application in applications)
                {
                    SchedulerUtil.AddScheduler(application.Id);
                }
            }

            services.AddLiteDBIdentity("Filename=db/user.db;Connection=Shared").AddDefaultTokenProviders();
            services.AddDataProtection().PersistKeysToFileSystem(new System.IO.DirectoryInfo(@"/app"));
            // services.AddSingleton<LiteDbContext>();
            // services.AddSingleton<ILiteDbContext, LiteDbContext>(x => new LiteDbContext(liteDatabase));
            //
            // services.AddIdentity<ApplicationUser, AspNetCore.Identity.LiteDB.IdentityRole>(options =>
            //     {
            //         options.Password.RequireDigit = false;
            //         options.Password.RequireUppercase = false;
            //         options.Password.RequireLowercase = false;
            //         options.Password.RequireNonAlphanumeric = false;
            //         options.Password.RequiredLength = 6;
            //     })
            //     //.AddEntityFrameworkStores<ApplicationDbContext>()
            //     .AddUserStore<LiteDbUserStore<ApplicationUser>>()
            //     .AddRoleStore<LiteDbRoleStore<AspNetCore.Identity.LiteDB.IdentityRole>>()
            //     .AddDefaultTokenProviders();

            services.ConfigureApplicationCookie(options =>
            {
                options.AccessDeniedPath   = "/Account/AccessDenied";
                options.Cookie.Name        = "renewal";
                options.LoginPath          = "/User/Login";
                options.LogoutPath         = "/User/Logout";
                options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
                options.SlidingExpiration  = true;
            });
            services.AddAuthentication(configureOptions =>
            {
            })
            .AddCookie(cookieOptions =>
            {
                cookieOptions.LoginPath        = new PathString("/Home/Login");
                cookieOptions.LogoutPath       = new PathString("/Home/Logout");
                cookieOptions.AccessDeniedPath = new PathString("/Home/Error");
            })
            .AddMicrosoftAccount(microsoftOptions =>
            {
                microsoftOptions.ClientId     = Configuration["Authentication:Microsoft:ClientId"];
                microsoftOptions.ClientSecret = Configuration["Authentication:Microsoft:ClientSecret"];
                //microsoftOptions.CallbackPath = new PathString("/signin-microsoft");
            })
            .AddGitHub(builder =>
            {
                builder.ClientId     = Configuration["Authentication:Github:ClientId"];
                builder.ClientSecret = Configuration["Authentication:Github:ClientSecret"];
                builder.Scope.Add("user:email");
            });

            services.Configure <ForwardedHeadersOptions>(options =>
            {
                options.ForwardedHeaders =
                    ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
            });

            services.AddControllersWithViews();
        }