예제 #1
0
        public ActionResult AgregarFotoPorfolio(EditarUsuarioModel modelo)
        {
            if (ModelState.IsValid)
            {
                HttpPostedFileBase file = modelo.File;

                Usuario usuarioActual = ObtenerUsuarioActual(User);

                if (file != null && file.ContentLength > 0)
                {
                    Guid   guid = Guid.NewGuid();
                    string str  = guid.ToString();

                    string path = Path.Combine(Server.MapPath("~/PorfoliosPictures"), str + ".jpg");

                    using (MemoryStream outStream = new MemoryStream())
                    {
                        using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
                        {
                            Size size = new Size(1000, 1000);
                            ISupportedImageFormat format = new JpegFormat {
                                Quality = 80
                            };
                            imageFactory.Load(file.InputStream)
                            .Constrain(size)
                            .BackgroundColor(Color.White)
                            .Format(format)
                            .Save(path);
                        }
                    }


                    //crear nueva foto porfolio
                    FotoPorfolio foto = new FotoPorfolio();
                    foto.Usuario   = usuarioActual;
                    foto.UsuarioId = usuarioActual.Id;
                    foto.Url       = str;

                    //agregarla a la lista
                    //if (usuarioActual.Fotos == null)
                    //{
                    // usuarioActual.Fotos = new List<FotoPorfolio>();
                    //}
                    usuarioActual.Fotos.Add(foto);

                    //db.Usuarios.Add(usuarioActual);
                    //db.SaveChanges();

                    db.FotosPorfolio.Add(foto);
                    db.SaveChangesAsync();


                    return(RedirectToAction("Index"));

                    //return View("Usuario", usuarioActual);
                }
            }
            return(RedirectToAction("Index"));
        }
        public async System.Threading.Tasks.Task <ActionResult> AceptarPostulacion(int id)
        {
            Postulacion postulacion = await db.Postulaciones.FindAsync(id);

            Usuario usuario = ObtenerUsuarioActual(User);

            if (postulacion != null && usuario.Id == postulacion.Propuesta.Creador.Id)
            {
                postulacion.Propuesta.UsuarioGanador   = postulacion.Usuario;
                postulacion.Propuesta.UsuarioGanadorId = postulacion.Usuario.Id;
                postulacion.Propuesta.Activa           = false;
                db.Entry(postulacion).State            = EntityState.Modified;
                await db.SaveChangesAsync();
            }

            //ir al dashboard

            return(RedirectToRoute("/Dashboard"));
        }
        public async Task <RefreshToken> GetTokenForUserAsync(IdentityUser user)
        {
            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_jwtSettings.Secret);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new[]
                {
                    new Claim(JwtRegisteredClaimNames.Sub, user.Email),
                    new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                    new Claim(JwtRegisteredClaimNames.Email, user.Email),
                    new Claim("id", user.Id)
                }),
                Expires            = DateTime.UtcNow.Add(_jwtSettings.TokenLifeTime),
                SigningCredentials =
                    new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256)
            };

            var token = tokenHandler.CreateToken(tokenDescriptor);

            var refreshToken = new RefreshTokenDto
            {
                JwtId        = token.Id,
                UserId       = user.Id,
                CreationDate = DateTime.UtcNow,
                ExpiryDate   = DateTime.UtcNow.Add(_jwtSettings.RefreshTokenLifeTime),
            };

            await _context.RefreshTokens.AddAsync(refreshToken);

            await _context.SaveChangesAsync();

            return(new RefreshToken
            {
                Token = tokenHandler.WriteToken(token),
                Refresh = refreshToken.Token
            });
        }
예제 #4
0
 private async Task<IdentityResult> ChangeEmailAsync(string v, string newEmail)
 {
     var db = new IdentityDb();
     var user = db.Users.FirstOrDefaultAsync(x => x.Id == User.Identity.GetUserId());
     user.Result.Email = newEmail;
     await db.SaveChangesAsync();
     return await Task.FromResult(IdentityResult.Success);
 }
        public async Task <AuthenticationResponse> RefreshTokenAsync(string token, string refreshToken)
        {
            var validatedToken = _principalProvider.GetPrincipalFromToken(token);

            if (validatedToken == null)
            {
                return(InvalidTokenResponse());
            }

            var expiryDateUnix =
                long.Parse(validatedToken.Claims.Single(x => x.Type == JwtRegisteredClaimNames.Exp).Value);

            var expiryDateTimeUtc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
                                    .AddSeconds(expiryDateUnix);

            if (expiryDateTimeUtc > DateTime.UtcNow)
            {
                return(InvalidTokenResponse());
            }

            var jti = validatedToken.Claims.Single(x => x.Type == JwtRegisteredClaimNames.Jti).Value;

            var storedRefreshToken = await _context.RefreshTokens.SingleOrDefaultAsync(x => x.Token == refreshToken);

            if (storedRefreshToken == null)
            {
                return(InvalidTokenResponse());
            }

            if (DateTime.UtcNow > storedRefreshToken.ExpiryDate)
            {
                return(InvalidTokenResponse());
            }

            if (storedRefreshToken.Invalidated)
            {
                return(InvalidTokenResponse());
            }

            if (storedRefreshToken.Used)
            {
                return(InvalidTokenResponse());
            }

            if (storedRefreshToken.JwtId != jti)
            {
                return(InvalidTokenResponse());
            }

            storedRefreshToken.Used = true;
            _context.RefreshTokens.Update(storedRefreshToken);
            await _context.SaveChangesAsync();

            var user = await _userManager.FindByIdAsync(validatedToken.Claims.Single(x => x.Type == "id").Value);

            var userToken = await _tokenProvider.GetTokenForUserAsync(user);

            return(new AuthenticationResponse
            {
                Success = true,
                Token = userToken.Token,
                RefreshToken = userToken.Refresh
            });
        }