Ejemplo n.º 1
0
        //用户注册
        public async Task <IActionResult> POSTAsync([FromBody] UserViewModel usrvm)
        {
            if (usrvm == null)
            {
                return(new StatusCodeResult(500));
            }
            else
            {
                ResultViewModel resultModel = new ResultViewModel();
                try
                {
                    ApplicationUser user = await userManage.FindByNameAsync(usrvm.UserName);

                    bool isExist = user != null ? await userManage.FindByEmailAsync(usrvm.UserName) != null : false;

                    if (isExist)
                    {
                        resultModel.IsSuccess = false; resultModel.Message = "已存在此账号";
                    }
                    else
                    {
                        var CreateDate = DateTime.Now;
                        user = new ApplicationUser()
                        {
                            UserName         = usrvm.UserName,
                            PasswordHash     = usrvm.UserPwd,
                            CreateDate       = CreateDate,
                            LastModifiedDate = CreateDate,
                            EmailConfirmed   = false,
                            LockoutEnabled   = false,
                        };

                        var result = await userManage.CreateAsync(user, usrvm.UserPwd);

                        var result2 = await userManage.AddToRoleAsync(user, "Registered");

                        if (result.Succeeded && result2.Succeeded)
                        {
                            var    jwt       = tokenHelper.CreateJWTToken(user.Id);
                            string tokenJson = JsonConvert.SerializeObject(jwt);
                            resultModel.IsSuccess = true;
                            resultModel.Token     = tokenJson;
                        }
                        else
                        {
                            resultModel.IsSuccess = false; resultModel.Message = result.Errors.FirstOrDefault().Description;
                        }
                    }
                }
                catch (Exception e)
                {
                    resultModel.IsSuccess = false;
                    resultModel.Message   = e.Message;
                }
                return(new JsonResult(resultModel, DefaultJsonSettings));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        public async Task Login(HttpContext httpContext)
        {
            ApplicationUser user        = null;
            ResultViewModel resultModel = new ResultViewModel();

            try
            {
                string userName = httpContext.Request.Form["userName"];
                string psw      = httpContext.Request.Form["userPwd"];
                using (var scope = serviceProvider.CreateScope())
                {
                    using (var UserManager = scope.ServiceProvider.GetService <UserManager <ApplicationUser> >())
                    {
                        user = await UserManager.FindByNameAsync(userName);

                        if (user == null && userName.Contains("@"))
                        {
                            user = await UserManager.FindByEmailAsync(userName);
                        }
                        //请求中的密码是否和指定用户的密码一致
                        var sucess = user != null && await UserManager.CheckPasswordAsync(user, psw);

                        if (sucess)
                        {
                            tokenHelper = scope.ServiceProvider.GetService <TokenHelper>();
                            var jwt = tokenHelper.CreateJWTToken(user.Id);
                            resultModel.IsSuccess = true;
                            resultModel.Token     = JsonConvert.SerializeObject(jwt);;
                        }
                        else
                        {
                            resultModel.IsSuccess = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                resultModel.IsSuccess = false;
                resultModel.Message   = ex.Message;
            }
            httpContext.Response.ContentType = "application/json";
            await httpContext.Response.WriteAsync(JsonConvert.SerializeObject(resultModel));

            return;
        }