Ejemplo n.º 1
0
        public async Task <IActionResult> RegisterUser([FromBody] RegisterUserDto model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = new ApplicationUser
            {
                UserName = model.Email,
                Email    = model.Email,
                IsActive = true
            };

            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                var profile = new Model.UserProfile
                {
                    AppUser   = user,
                    FirstName = model.FirstName,
                    LastName  = model.LastName
                };
                _userProfileService.Create(profile);
            }
            else
            {
                return(GetErrorResult(result));
            }

            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            return(Ok(new { userId = user.Id, confirmCode = HttpUtility.UrlEncode(code) }));
        }
Ejemplo n.º 2
0
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            if (!Request.Headers.ContainsKey("Authorization"))
            {
                return(AuthenticateResult.Fail("Missing Authorization Header"));
            }

            Model.UserProfile        user  = null;
            Model.AdministratorLogin admin = null;
            try
            {
                var authHeader      = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
                var credentialBytes = Convert.FromBase64String(authHeader.Parameter);
                var credentials     = Encoding.UTF8.GetString(credentialBytes).Split(':');
                var username        = credentials[0];
                var password        = credentials[1];
                user  = _userService.Authenticate(username, password);
                admin = _adminService.Authenticate(username, password);
            }
            catch
            {
                return(AuthenticateResult.Fail("Invalid Authorization Header"));
            }

            if (user == null && admin == null)
            {
                return(AuthenticateResult.Fail("Invalid Username or Password"));
            }
            if (admin != null)
            {
                var claims = new List <Claim> {
                    new Claim(ClaimTypes.NameIdentifier, admin.AdminName),
                    new Claim(ClaimTypes.Email, admin.AdminName + "@mdome.com"),
                };
                claims.Add(new Claim(ClaimTypes.Role, "Admin"));
                var identity  = new ClaimsIdentity(claims, Scheme.Name);
                var principal = new ClaimsPrincipal(identity);
                var ticket    = new AuthenticationTicket(principal, Scheme.Name);
                return(AuthenticateResult.Success(ticket));
            }
            else
            {
                var claims = new List <Claim> {
                    new Claim(ClaimTypes.NameIdentifier, user.Username),
                    new Claim(ClaimTypes.Email, user.Email),
                };
                claims.Add(new Claim(ClaimTypes.Role, "User"));
                var identity  = new ClaimsIdentity(claims, Scheme.Name);
                var principal = new ClaimsPrincipal(identity);
                var ticket    = new AuthenticationTicket(principal, Scheme.Name);
                return(AuthenticateResult.Success(ticket));
            }
        }
Ejemplo n.º 3
0
        public override void LoadFromEntity(Model.UserProfile entity)
        {
            base.LoadFromEntity(entity);

            FirstName = entity.FirstName;
            LastName  = entity.LastName;

            if (entity.AppUser != null)
            {
                Email = entity.AppUser.Email;
            }
        }
Ejemplo n.º 4
0
        public void SetProfile(Model.UserProfile profile)
        {
            //Buscando um user profile para saber se existe no banco
            var pro = contextDB.UserProfile.FirstOrDefault(u => u.Id == profile.Id);

            //Se existir no banco de dados
            if (pro != null)
            {
                pro.Id      = profile.Id;
                pro.Visitas = profile.Visitas;

                contextDB.Entry(pro).State = System.Data.Entity.EntityState.Modified; //Mude o estado do objeto para atualização
            }
            else //Senão incluí um objeto novo e adicione ao banco de dados
            {
                contextDB.Set <Model.UserProfile>().Add(profile);
            }
            //Faz o commit do banco de dados
            contextDB.SaveChanges();
        }