2022-09-08 08:04:32 -07:00

141 lines
5.8 KiB
C#

using System;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.Google;
using Owin;
using Chruch.Net.Models;
using Church.Net.DAL.EF;
using Church.Net.Entity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace Chruch.Net
{
public partial class Startup
{
// 如需設定驗證的詳細資訊,請瀏覽 https://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// 設定資料庫內容、使用者管理員和登入管理員,以針對每個要求使用單一執行個體
app.CreatePerOwinContext(ChurchNetContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// 讓應用程式使用 Cookie 儲存已登入使用者的資訊
// 並使用 Cookie 暫時儲存使用者利用協力廠商登入提供者登入的相關資訊;
// 在 Cookie 中設定簽章
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// 讓應用程式在使用者登入時驗證安全性戳記。
// 這是您變更密碼或將外部登入新增至帳戶時所使用的安全性功能。
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, FamilyMember>(
validateInterval: TimeSpan.FromDays(300),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// 讓應用程式在雙因素驗證程序中驗證第二個因素時暫時儲存使用者資訊。
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
// 讓應用程式記住第二個登入驗證因素 (例如電話或電子郵件)。
// 核取此選項之後,將會在用來登入的裝置上記住登入程序期間的第二個驗證步驟。
// 這類似於登入時的 RememberMe 選項。
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
// 註銷下列各行以啟用利用協力廠商登入提供者登入
//app.UseMicrosoftAccountAuthentication(
// clientId: "",
// clientSecret: "");
//app.UseTwitterAuthentication(
// consumerKey: "",
// consumerSecret: "");
//app.UseFacebookAuthentication(
// appId: "",
// appSecret: "");
//app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
//{
// ClientId = "",
// ClientSecret = ""
//});
CreateRolesandUsers();
}
private void CreateRolesandUsers()
{
ChurchNetContext context = new ChurchNetContext();
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var UserManager = new UserManager<FamilyMember>(new UserStore<FamilyMember>(context));
// In Startup iam creating first Admin Role and creating a default Admin User
if (!roleManager.RoleExists("Admin"))
{
// first we create Admin rool
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "Admin";
roleManager.Create(role);
//Here we create a Admin super user who will maintain the website
var user = new FamilyMember();
user.UserName = "chris";
user.Email = "yuanson.chen@gmail.com";
user.Birthday = new DateTime(1990, 01, 29);
user.DateOfWalkIn = new DateTime(2018, 05, 25);
string userPWD = "6262263816";
var chkUser = UserManager.Create(user, userPWD);
//Add default User to Role Admin
if (chkUser.Succeeded)
{
var result1 = UserManager.AddToRole(user.Id, "Admin");
}
}
// creating Creating Manager role
if (!roleManager.RoleExists("DirectorPastor"))
{
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "DirectorPastor";
roleManager.Create(role);
}
if (!roleManager.RoleExists("Pastor"))
{
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "Pastor";
roleManager.Create(role);
}
// creating Creating Employee role
if (!roleManager.RoleExists("FamilyMember"))
{
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "FamilyMember";
roleManager.Create(role);
}
if (!roleManager.RoleExists("CellGroupLeader"))
{
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "CellGroupLeader";
roleManager.Create(role);
}
}
}
}