Пример #1
0
        public IActionResult CreateToken([FromBody] User user)
        {
            string[] roles = { "admin", "user" };
            var      token = tokenHelper.CreateToken(user, roles);

            return(Ok(token));
        }
        private string CreateToken(Users user)
        {
            var claimIdentity = new ClaimsIdentity(new Claim[] {
                new Claim("UserId", user.Id.ToString()),
                new Claim("RoleId", user.RoleId.ToString()),
                new Claim("UserName", user.UserName)
            });

            var token = JwtTokenHelper.CreateToken(
                claimIdentity,
                Settings.TokenExpire,
                Settings.JwtSecretKey
                );

            return(token);
        }
Пример #3
0
        public IHttpActionResult Login([FromBody] LoginCredentials credentials)
        {
            bool valid = _context.Users.CheckCredentials(credentials);

            // If credentials are valid, create token and return OK response code
            if (valid)
            {
                var    user  = _context.Users.GetByUsername(credentials.Username);
                string token = JwtTokenHelper.CreateToken(user.ID);
                return(Ok(new LoginResponse {
                    Token = token
                }));
            }

            // If not valid, throw error
            throw new ValidationException("Invalid login credentials!");
        }
Пример #4
0
        public async Task <IHttpActionResult> Post(AuthorizeRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var result = await authorizedAppProvider.GetAuth(request.AppToken, request.AppSecret);

            if (result.IsSuccess)
            {
                var token = jwtTokenHelper.CreateToken(result.AuthorizedApp);
                return(Ok(token));
            }

            return(Unauthorized());
        }
Пример #5
0
        public async Task <IActionResult> Login([FromBody] Login model)
        {
            var user = await userManager.FindByNameAsync(model.Username);

            if (user != null && await userManager.CheckPasswordAsync(user, model.Password))
            {
                string secretKey = configuration.GetValue <string>("JWTSecretKey");
                string token     = JwtTokenHelper.CreateToken(user.UserName, secretKey);

                return(Ok(new
                {
                    token = token,
                }));
            }

            return(Unauthorized());
        }
        public IHttpActionResult Post(AuthorizeRequestDto request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var authApp = _db.AuthorizedApps
                          .FirstOrDefault(i => i.AppToken == request.AppToken &&
                                          i.AppSecret == request.AppSecret &&
                                          DateTime.UtcNow < i.TokenExpiration);

            if (authApp == null)
            {
                return(Unauthorized());
            }

            var token = _tokenHelper.CreateToken(authApp);

            return(Ok(token));
        }
Пример #7
0
        public async Task SendRequestForgottenPassword(ForgottenUserPasswordRequest request)
        {
            Users user = await _repository.GetDataAsync(x => x.UserName == request.UserName);

            if (user == null)
            {
                throw new Exception("User not found");
            }

            var claimIdentity = new ClaimsIdentity(new Claim[] {
                new Claim("UserId", user.Id.ToString()),
                new Claim("RoleId", user.RoleId.ToString()),
                new Claim("UserName", user.UserName)
            });

            var token = JwtTokenHelper.CreateToken(
                claimIdentity,
                Settings.TokenExpire,
                Settings.JwtSecretKey
                );

            using (var client = new WebClient())
            {
                var path     = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + Settings.ForgottenPasswordTemplate;
                var htmlCode = client.DownloadString(path);
                var url      = string.Format("{0}{1}{2}?token={3}", Settings.CorsDomain, Settings.Domain, Settings.ResetPasswordUrl, token);
                var body     = htmlCode.Replace("_@1_", url);

                var mailRequest = new MailRequest()
                {
                    Body      = body,
                    Subject   = "Mot de passe oublié",
                    Recipient = user.UserName
                };

                MailHelper.SendMailSuccess(mailRequest);
            }
        }