Beispiel #1
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());
        }
Beispiel #2
0
        public IActionResult Index(JWTViewModel body)
        {
            var jwt     = body.JWTString;
            var handler = new JwtSecurityTokenHandler();
            var token   = handler.ReadJwtToken(jwt);

            body.Decoded = token.ToString();

            TempData["model"] = JsonConvert.SerializeObject(body);;
            return(RedirectToAction(nameof(JWTController.Index), body));
        }
Beispiel #3
0
        public IActionResult Index()
        {
            JWTViewModel model = null;

            if (TempData["model"] != null)
            {
                model = JsonConvert.DeserializeObject <JWTViewModel>((string)TempData["model"]);
                var headerAndPayload = model.Decoded.Split(new char[] { '.' });
                model.Header  = headerAndPayload[0];
                model.Payload = headerAndPayload[1];
                return(View(model));
            }
            else if (TempData["encoded"] != null)
            {
                model           = new JWTViewModel();
                model.JWTString = (string)TempData["encoded"];
                return(View(model));
            }

            else
            {
                return(View(new JWTViewModel()));
            }
        }