Esempio n. 1
0
        /// <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;
                }
            }
        }
Esempio n. 2
0
        /// <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;
        }