Esempio n. 1
0
        public async Task <IActionResult> GetToken([FromBody] AuthBindingModel model)
        {
            if (ModelState.IsValid)
            {
                User user = await UserMgr.FindByEmailAsync(model.Email);

                if (user != null)
                {
                    var signInResult = await SignInMgr.CheckPasswordSignInAsync(user, model.Password.ToString(), false);

                    if (signInResult.Succeeded)
                    {
                        var roles = await UserMgr.GetRolesAsync(user);

                        return(generateToken(user, roles.FirstOrDefault()));
                    }
                    else
                    {
                        var err2 = new { status = "error", message = "Authentication Failed ! Check Email & Password" };
                        return(BadRequest(err2));
                    }
                }

                var err = new { status = "error", message = "Could not find a user for given Email!" };
                return(BadRequest(err));
            }

            return(BadRequest());
        }
Esempio n. 2
0
        public async Task <IActionResult> GenerateToken([FromBody] JWTViewModel m)
        {
            if (ModelState.IsValid)
            {
                AppUser user = await UserMgr.FindByNameAsync(m.UserName);

                if (user != null)
                {
                    var signInResult = await SignInMgr.CheckPasswordSignInAsync(user, m.Password.ToString(), false);

                    if (signInResult.Succeeded)
                    {
                        var key  = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(AppSettings.JWT_Secret));
                        var cred = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

                        var claims = new[]
                        {
                            new Claim(JwtRegisteredClaimNames.Sub, m.UserName),
                            new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                            new Claim("UserType", user.Type),
                            new Claim("UserName", m.UserName)
                        };

                        var token = new JwtSecurityToken(

                            JWT.Iss,
                            JWT.Aud,
                            claims,
                            expires: DateTime.UtcNow.AddMinutes(30),
                            signingCredentials: cred

                            );

                        var results = new
                        {
                            token      = new JwtSecurityTokenHandler().WriteToken(token),
                            expiration = token.ValidTo
                        };
                        await LogChangeAsync(user, "Login");

                        return(Ok(results));
                    }
                    else
                    {
                        var err2 = new { status = "error", message = "Authentication Failed ! Check UserName & Password" };
                        return(BadRequest(err2));
                    }
                }

                var err = new { status = "error", message = "Could not find a user!" };
                return(BadRequest(err));
            }

            return(BadRequest());
        }
Esempio n. 3
0
        public async Task <IActionResult> GenerateToken([FromBody] AuthBindingModel model)
        {
            if (ModelState.IsValid)
            {
                User user = await UserMgr.FindByEmailAsync(model.Email);

                if (user != null)
                {
                    var signInResult = await SignInMgr.CheckPasswordSignInAsync(user, model.Password.ToString(), false);

                    if (signInResult.Succeeded)
                    {
                        var key  = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JWTSettings.Secret));
                        var cred = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

                        var claims = new[]
                        {
                            new Claim(JwtRegisteredClaimNames.Sub, user.Email),
                            new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                            new Claim("UserType", ""),
                        };

                        var token = new JwtSecurityToken(

                            JWTSettings.Issuer,
                            JWTSettings.Audience,
                            claims,
                            expires: DateTime.UtcNow.AddMinutes(JWTSettings.ExpieryTimeInMins),
                            signingCredentials: cred

                            );

                        var results = new
                        {
                            thisuser   = new { UserName = user.UserName, Email = user.Email },
                            token      = new JwtSecurityTokenHandler().WriteToken(token),
                            expiration = token.ValidTo
                        };
                        return(Ok(results));
                    }
                    else
                    {
                        var err2 = new { status = "error", message = "Authentication Failed ! Check UserName & Password" };
                        return(BadRequest(err2));
                    }
                }

                var err = new { status = "error", message = "Could not find a user!" };
                return(BadRequest(err));
            }

            return(BadRequest());
        }