Пример #1
0
        public async Task <IActionResult> login([FromBody] userManagement user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            if (user.DeviceId == null || user.Model == null || user.Manufacturer == null)
            {
                return(BadRequest());
            }
            var u = await _userManager.FindByEmailAsync(user.Email);

            var signInResult = await _signInManager.CheckPasswordSignInAsync(u, user.Password, false);

            if (!signInResult.Succeeded)
            {
                return(BadRequest());
            }
            if (phoneInUse(user.DeviceId))
            {
                if (!userOwnsPhone(u.Id, user.DeviceId))
                {
                    return(Unauthorized());
                }
            }


            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JWTConstants.Key));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var claims = new[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, user.Email),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim(JwtRegisteredClaimNames.UniqueName, user.Email)
            };
            var token = new JwtSecurityToken(JWTConstants.Issuer, JWTConstants.Audience, claims, expires: DateTime.UtcNow.AddDays(1000), signingCredentials: creds);

            var result = new
            {
                token      = new JwtSecurityTokenHandler().WriteToken(token),
                expiration = token.ValidTo
            };
            var usr = await _userManager.FindByEmailAsync(user.Email);

            var phone = new Phone()
            {
                DeviceId = user.DeviceId, Model = user.Model, Manufacturer = user.Manufacturer, UserRef = usr.Id, EncryptionKey = getKey()
            };

            try
            {
                _context.Add(phone);
                _context.SaveChanges();
            }
            catch (DbUpdateException ex)
            {
            }
            return(Created("", result));
        }
Пример #2
0
        public async Task <IActionResult> register([FromBody] userManagement user)
        {
            if (user.Email == null || user.Password == null)
            {
                return(BadRequest());
            }
            var usr = new ApplicationUser {
                UserName = user.Email, Email = user.Email, EmailConfirmed = true
            };
            var result = await _userManager.CreateAsync(usr, user.Password);

            if (!result.Succeeded)
            {
                return(BadRequest());
            }

            return(Ok());
        }