コード例 #1
0
        public async Task <object> Register([FromBody] RegisterDto model)
        {
            if (!ModelState.IsValid)
            {
                var errrors = AuthValidator.GetErrorsByModel(ModelState);
                return(BadRequest(errrors));
            }
            var user = new IdentityUser
            {
                UserName = model.Email,
                Email    = model.Email
            };
            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                await _signInManager.SignInAsync(user, false);

                return(Ok(await GenerateJwtToken(model.Email, user)));
            }
            else
            {
                return(BadRequest(result.Errors));
            }
        }
コード例 #2
0
        public async Task <IActionResult> AddTask([FromBody] TaskDto task)
        {
            if (!ModelState.IsValid)
            {
                var errrors = AuthValidator.GetErrorsByModel(ModelState);
                return(BadRequest(errrors));
            }
            try
            {
                string userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;

                DAL.Entities.Task newtask = new DAL.Entities.Task()
                {
                    ApplicationUserId = userId, Category = _context.Categories.FirstOrDefault(x => x.CategoryName == task.Category), date = task.date, Description = task.Description, Name = task.Name, IsComplited = task.IsComplited
                };
                _context.Tasks.Add(newtask);
                await _context.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
コード例 #3
0
        public async Task <object> Login([FromBody] LoginDto model)
        {
            if (!ModelState.IsValid)
            {
                var errrors = AuthValidator.GetErrorsByModel(ModelState);
                return(BadRequest(errrors));
            }
            var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false);

            if (result.Succeeded)
            {
                var appUser = new IdentityUser
                {
                    Email = model.Email
                };
                return(await GenerateJwtToken(model.Email, appUser));
            }
            else
            {
                return(BadRequest(new { invalid = "You entered an invalid credentials!" }));
            }
        }