Ejemplo n.º 1
0
 public IEnumerable <PodUser> GetPodUser()
 {
     using (var db = new PodTrackdbContext())
     {
         return(db.PodUser);
     }
 }
Ejemplo n.º 2
0
        public User Authenticate(string username, string password)
        {
            using (var db = new PodTrackdbContext())
            {
                var podUser = db.PodUser.SingleOrDefault(x => x.Username == username);

                var passwordMatch = PasswordStorage.VerifyPassword(password, podUser.Password);
                // return null if user not found
                if (podUser == null || !passwordMatch)
                {
                    return(null);
                }

                var user = new User
                {
                    Username = podUser.Username,
                    Id       = podUser.Id,
                    Password = podUser.Password
                };

                // authentication successful so generate jwt token
                var tokenHandler    = new JwtSecurityTokenHandler();
                var key             = Encoding.ASCII.GetBytes(_appSettings.Secret);
                var tokenDescriptor = new SecurityTokenDescriptor
                {
                    Subject = new ClaimsIdentity(new Claim[]
                    {
                        new Claim(ClaimTypes.Name, user.Username.ToString()),
                        new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())
                    }),
                    Expires            = DateTime.UtcNow.AddDays(7),
                    SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
                };
                var token = tokenHandler.CreateToken(tokenDescriptor);
                user.Token = tokenHandler.WriteToken(token);

                // remove password before returning
                user.Password = null;

                return(user);
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Register([FromBody] PodUser podUser)
        {
            using (var db = new PodTrackdbContext())
            {
                if (db.PodUser.FirstOrDefault(x => x.Username == podUser.Username) != null)
                {
                    return(BadRequest(new { message = "Username already exists." }));
                }

                podUser.Password = PasswordStorage.CreateHash(podUser.Password);

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                db.PodUser.Add(podUser);
                await db.SaveChangesAsync();

                return(Ok());
            }
        }
 public PodcastEpisodesController(PodTrackdbContext context)
 {
     _context = context;
 }
Ejemplo n.º 5
0
 public FollowedPodcastsController(PodTrackdbContext context)
 {
     _context = context;
 }