/// <summary> /// 重设密码 /// </summary> /// <param name="email">邮箱</param> /// <param name="newPassword">新密码</param> /// <returns></returns> public async Task<bool> ResetPasswordAsync(string email, string newPassword) { using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { UserAccountHandler uaHandler = new UserAccountHandler(dbContext); return await uaHandler.ResetPasswordAsync(email, newPassword); } }
/// <summary> /// 退出 /// </summary> /// <param name="userName">用户名</param> /// <returns></returns> public async Task<SignOutStatus> SignOutAsync(string userName, string token) { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { UserAccountHandler uaHandler = new UserAccountHandler(dbContext); return await uaHandler.SignOutAsync(userName, token); } }
/// <summary> /// 登录 /// </summary> /// <param name="userName">用户名</param> /// <param name="password">密码</param> /// <returns></returns> public async Task<Tuple<KoalaBlogIdentityObject, SignInStatus, string>> SignInAsync(string userName, string password, bool isPersistent) { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { UserAccountHandler uaHandler = new UserAccountHandler(dbContext); return await uaHandler.SignInAsync(userName, password, isPersistent); } }
/// <summary> /// 创建Person并且建立UserAccountXPerson的关系 /// </summary> /// <param name="ua">UserAccount</param> /// <returns></returns> public async Task<Person> CreatePersonAsync(UserAccount ua) { AssertUtil.IsNotNull(ua, "UserAccount can't be null"); UserAccountHandler uaHandler = new UserAccountHandler(_dbContext); UserAccountXPersonHandler uaxpHandler = new UserAccountXPersonHandler(_dbContext); AssertUtil.IsNotNull(await uaHandler.GetByIdAsync(ua.ID), "This user account doesn't exist"); //1. Check whether the existing relationships. AssertUtil.IsTrue(await uaxpHandler.AnyAsync(x => x.UserAccountID == ua.ID), "Existing relationships"); using(var dbTransaction = _dbContext.Database.BeginTransaction()) { try { //2. Setup the basic profile. Person per = new Person(); per.NickName = ua.UserName; per.RealNameAccessLevel = PersonInfoAccessInfo.MyselfOnly; per.SexualTrendAccessLevel = PersonInfoAccessInfo.MyselfOnly; per.MaritalStatusAccessLevel = PersonInfoAccessInfo.MyselfOnly; per.QQAccessLevel = PersonInfoAccessInfo.MyselfOnly; per.DOBAccessLevel = PersonInfoAccessInfo.MyselfOnly; per.BloodTypeAccessLevel = PersonInfoAccessInfo.MyselfOnly; per.HomePageAccessLevel = PersonInfoAccessInfo.MyselfOnly; per.AllowablePersonForComment = AllowablePersonForComment.All; per.AllowCommentAttachContent = true; Add(per); await SaveChangesAsync(); UserAccountXPerson uaxp = new UserAccountXPerson(); uaxp.UserAccountID = ua.ID; uaxp.PersonID = per.ID; uaxpHandler.Add(uaxp); await SaveChangesAsync(); dbTransaction.Commit(); return per; } catch (Exception) { dbTransaction.Rollback(); throw; } } }
public async Task<bool> ConfirmEmailAsync(string email, string code) { AssertUtil.Waterfall() .NotNullOrWhiteSpace(email, "邮箱不能为空") .NotNullOrWhiteSpace(code, "验证码不能为空") .IsValidEmail(email, "邮箱地址不正确") .Done(); UserAccountHandler uaHandler = new UserAccountHandler(_dbContext); PersonHandler perHandler = new PersonHandler(_dbContext); AvatarHandler avatarHandler = new AvatarHandler(_dbContext); UserAccount user = await uaHandler.GetByEmailAsync(email); if (user != null) { //1. 判断验证码是否匹配。 bool isMatched = await Entities.AnyAsync(x => x.UserAccountID == user.ID && x.Code == code); if (isMatched) { //这里需要用事务来保证执行成功。 using(TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { //2. 匹配则修改邮件验证为True。 user.EmailConfirmed = true; uaHandler.MarkAsModified(user); bool isSucceed = await SaveChangesAsync() > 0; //3. 同时生成UserAccountXPerson记录。 if (isSucceed) { Person per = await perHandler.CreatePersonAsync(user); //4. 生成默认Avatar。 await avatarHandler.CreateDefaultAvatar(per.ID); } transactionScope.Complete(); return true; } } } return false; }
/// <summary> /// 注册 /// </summary> /// <param name="userName">用户名</param> /// <param name="password">密码</param> /// <param name="email">邮箱</param> /// <returns></returns> public async Task<Tuple<UserAccount, RegisterStatus>> RegisterAsync(string userName, string password, string email) { using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { UserAccountHandler uaHandler = new UserAccountHandler(dbContext); RegisterStatus registerStatus = RegisterStatus.Failure; UserAccount registerUser = await uaHandler.CreateAsync(userName, password, email); if (registerUser != null) { registerStatus = RegisterStatus.Succeeded; } return new Tuple<UserAccount, RegisterStatus>(registerUser, registerStatus); } }
public void TestFixtureSetUp() { TestUtil.CleanUpData(); using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { UserAccountHandler uaHandler = new UserAccountHandler(dbContext); testUA1 = new UserAccount(); testUA1.UserName = "******"; testUA1.PasswordSalt = "testSalt1"; testUA1.Password = "******"; testUA1.Email = "*****@*****.**"; testUA1.LastLogon = DateTime.Now; testUA1.EmailConfirmed = true; testUA1.Status = UserAccount.STATUS_ACTIVE; uaHandler.Add(testUA1); uaHandler.SaveChanges(); } }
/// <summary> /// 验证Bearer Token /// </summary> /// <param name="userAccountId">用户ID</param> /// <param name="accessToken">令牌</param> /// <returns></returns> public async Task<IPrincipal> AuthenticateBearerTokenAsync(string accessToken) { //1. 根据条件获取Token对象。 Token bearerToken = await Fetch(x => x.AccessToken == accessToken && !x.IsRevoked && x.ExpirationDate > DateTime.Now).SingleOrDefaultAsync(); if(bearerToken != null) { //2. 如果Token对象不为空,则为Token验证成功,建立Principal。 KoalaBlogIdentityObject identityObj = new KoalaBlogIdentityObject(); UserAccountXPersonHandler uaxpHandler = new UserAccountXPersonHandler(_dbContext); //3. 获取UserAccountXPerson对象。 UserAccountXPerson uaxp = await uaxpHandler.LoadByUserAccountIDIncludeUserAccountAndPersonAsync(bearerToken.UserAccountID); if(uaxp != null) { if (uaxp.UserAccount != null) { identityObj.UserID = uaxp.UserAccount.ID; identityObj.UserName = uaxp.UserAccount.UserName; identityObj.Email = uaxp.UserAccount.Email; identityObj.Status = uaxp.UserAccount.Status; } if (uaxp.Person != null) { identityObj.PersonID = uaxp.Person.ID; identityObj.PersonNickName = uaxp.Person.NickName; identityObj.Introduction = uaxp.Person.Introduction; } } else { UserAccountHandler uaHandler = new UserAccountHandler(_dbContext); //4. 如果UserAccountXPerson对象为空,意味着可能是用户注册还没完成,则根据用户名获取UserAccount对象,赋值IdentityObject通用Property。 UserAccount userAccount = await uaHandler.GetByIdAsync(bearerToken.UserAccountID); if (userAccount != null) { identityObj.UserID = userAccount.ID; identityObj.UserName = userAccount.UserName; identityObj.Email = userAccount.Email; identityObj.Status = userAccount.Status; } } KoalaBlogIdentity identity = new KoalaBlogIdentity(identityObj); KoalaBlogPrincipal principal = new KoalaBlogPrincipal(identity); return principal; } return null; }
public async Task<bool> ResetPasswordConfirmEmailAsync(string email, string code) { AssertUtil.Waterfall() .NotNullOrWhiteSpace(email, "邮箱不能为空") .NotNullOrWhiteSpace(code, "验证码不能为空") .IsValidEmail(email, "邮箱地址不正确") .Done(); UserAccountHandler uaHandler = new UserAccountHandler(_dbContext); UserAccount user = await uaHandler.GetByEmailAsync(email); bool isMatched = false; if(user != null) { isMatched = await Entities.AnyAsync(x => x.UserAccountID == user.ID && x.Code == code && x.Type == EmailConfirmationType.ResetPassword); } return isMatched; }
/// <summary> /// 授权判断 /// </summary> /// <param name="userName">用户名</param> /// <param name="roleOrPermissionNames">角色或者权限名称</param> /// <returns></returns> public async Task<bool> IsUserInRoleAsync(string userName, string[] roleOrPermissionNames) { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { UserAccountHandler uaHandler = new UserAccountHandler(dbContext); return await uaHandler.IsUserInRoleAsync(userName, roleOrPermissionNames); } }
public async Task<UserAccount> GetSafeUserAccountByEmailAsync(string email) { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { UserAccountHandler uaHandler = new UserAccountHandler(dbContext); UserAccount user = await uaHandler.GetByEmailAsync(email); if(user != null) { user.CreatedBy = 0; user.CreatedDate = DateTime.MinValue; user.LastModifiedBy = 0; user.LastModifiedDate = DateTime.MinValue; user.UserName = string.Empty; user.Password = string.Empty; user.Status = string.Empty; user.LastLogon = DateTime.MinValue; } return user; } }
/// <summary> /// 根据UserName获取KoalaBlogIdentityObject /// </summary> /// <param name="userName"></param> /// <returns></returns> public async Task<KoalaBlogIdentityObject> GetIdentityObjectAsync() { using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { UserAccountXPersonHandler uaxpHandler = new UserAccountXPersonHandler(dbContext); if(CurrentThreadIdentityObject != null) { //1. 根据用户名获取UserAccountXPerson对象。 UserAccountXPerson uaxp = await uaxpHandler.LoadByUserNameAsync(CurrentThreadIdentityObject.UserName); if (uaxp != null) { KoalaBlogIdentityObject identityObject = new KoalaBlogIdentityObject(); if (uaxp.UserAccount != null) { identityObject.UserID = uaxp.UserAccount.ID; identityObject.UserName = uaxp.UserAccount.UserName; identityObject.Email = uaxp.UserAccount.Email; identityObject.Status = uaxp.UserAccount.Status; } if (uaxp.Person != null) { AvatarHandler avatarHandler = new AvatarHandler(dbContext); Avatar avatar = await avatarHandler.GetActiveAvatarByPersonId(uaxp.Person.ID); identityObject.PersonID = uaxp.Person.ID; identityObject.PersonNickName = uaxp.Person.NickName; identityObject.Introduction = uaxp.Person.Introduction; if (avatar != null) { identityObject.AvatarUrl = avatar.AvatarPath; } } return identityObject; } else { UserAccountHandler uaHandler = new UserAccountHandler(dbContext); //2. 如果UserAccountXPerson对象为空,意味着可能是用户注册还没完成,则根据用户名获取UserAccount对象,赋值IdentityObject通用Property。 UserAccount userAccount = await uaHandler.GetByUserNameAsync(CurrentThreadIdentityObject.UserName); if (userAccount != null) { KoalaBlogIdentityObject identityObject = new KoalaBlogIdentityObject() { UserID = userAccount.ID, UserName = userAccount.UserName, Email = userAccount.Email, Status = userAccount.Status }; return identityObject; } } } return null; } }