예제 #1
0
        // Called by the SDK to obtain a signed JWT, for a given contextId,
        // encapsulating context data to pass to Live Assist on chat creation
        public static String CreateJwt(string contextId)
        {
            var contextData = new ContextData()
            {
                customer = new Customer()
                {
                    firstName = new AssertedString()
                    {
                        value = "Sid"
                    },
                    lastName = new AssertedString()
                    {
                        value      = "James",
                        isAsserted = true
                    },
                    companySize = new AssertedInteger()
                    {
                        value = 10
                    }
                }
            };

            var jwt = Jwt.Create(contextId, contextData);

            return(jwt);
        }
예제 #2
0
        public async Task <IActionResult> PostAsync([FromBody] Models.Credentials credentials)
        {
            Models.Customer customer;
            if (AuthenticatedToken != null) //social auth
            {
                NewRelic.Api.Agent.NewRelic.AddCustomParameter("credentials.email", AuthenticatedToken.Payload["email"].ToString());
                customer = await _customerRepository.Get(AuthenticatedToken.Payload["email"].ToString());

                if (customer == null)
                {
                    customer = new Models.Customer
                    {
                        Nombre = AuthenticatedToken.Payload["name"].ToString(),
                        Mail   = AuthenticatedToken.Payload["email"].ToString(),
                        Tipo   = (int)Models.Credentials.Types.Social, //social user
                        Estado = 2,                                    //initial state
                        Condos = new List <Models.Condo>()
                    };
                    if (!await _customerRepository.CreateOrUpdate(customer))
                    {
                        return(new BadRequestObjectResult(customer)); //problems creating customer on db
                    }
                }
            }
            else
            {
                NewRelic.Api.Agent.NewRelic.AddCustomParameter("credentials.email", credentials.email);
                var getUserInfo = await _customerRepository.CheckPassword(credentials);

                if ((getUserInfo == null || getUserInfo.Tables.Count == 0 || getUserInfo.Tables[0].Rows.Count == 0))
                {
                    return(new UnauthorizedResult());
                }
                customer = getUserInfo.Tables[0].Select().ToCustomer();
            }

            if (customer.Estado > 2)
            {
                return(new ForbidResult()); //user disabled
            }
            var defaultDuration = !Request.Query.TryGetValue("tokenDuration", out StringValues customTokenDuration);
            var tokenDuration   = defaultDuration ? 5 : double.Parse(customTokenDuration);
            var jwt             = Jwt.Create(customer, tokenDuration);

            return(new OkObjectResult(new
            {
                email = customer.Mail,
                firstName = customer.Nombre,
                idToken = jwt,
                name = customer.Nombre,
                photoUrl = customer.Icono,
                provider = customer.Tipo == (int)Models.Credentials.Types.Social ? "social" : "internal",
                state = customer.Estado,
                data = customer.Condos,
                validTo = tokenDuration == 0 ? DateTime.MaxValue.ToUniversalTime().ToString() : DateTime.Now.AddMinutes(tokenDuration).ToUniversalTime().ToString()
            }));
        }
예제 #3
0
        public async Task <ApiResult <dynamic> > Login([FromBody] LoginRequest request)
        {
            var rsp = new ApiResult <dynamic>();

            if (!ModelState.IsValid)
            {
                rsp.StatusCode = 400;
                rsp.Message    = "Invalid Request";
                return(rsp);
            }

            var res = await _userService.LoginAsync(request);

            if (!res.Success)
            {
                rsp.Message    = res.Message;
                rsp.StatusCode = res.StatusCode;
                return(rsp);
            }

            var info = res.Data;

            if (!PasswordHash.PasswordHash.ValidatePassword(request.Password, info.Password))
            {
                rsp.Message = "用户名或密码错误!";
                return(rsp);
            }
            // 读取用户操作权限
            var operationResult = await _operationService.GetOperationnCodesByUserIdAsync(info.Id);

            // 如果存在数据
            if (operationResult.Success)
            {
                // 保存到缓存
                await _redisService.StringSetAsync <IList <string> >(info.Id, operationResult.Data);
            }
            var token = Jwt.Create(_tokenManagement, info);

            rsp.Message = "登录成功";
            rsp.Success = true;
            rsp.Data    = new
            {
                uuid        = info.Id,
                name        = info.RealName,
                token       = "Bearer " + token,
                permissions = operationResult.Success ? operationResult.Data : new List <string>()
            };
            return(rsp);
        }
예제 #4
0
        public static String CreateJwt(string contextId)
        {
            var contextData = new ContextData()
            {
                customer = new Customer()
                {
                    firstName = new AssertedString()
                    {
                        value = "VenkataSambasivaRao"
                    },
                    lastName = new AssertedString()
                    {
                        value = "Kesanam"
                    }
                }
            };

            return(Jwt.Create(contextId, contextData));
        }
예제 #5
0
        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            using (var db = new LiteDatabase(@"movieReservation.db")) {
                var col = db.GetCollection <Models.User>("users");

                // Get the user with the email
                var maybeUser = col.FindOne(x => x.Email == LoginRequest.Email);
                if (maybeUser == null)
                {
                    // User does not exist
                    Error = "Deze combinatie email en wachtwoord is onjuist.";
                    return(Page());
                }

                // Validate password is correct
                var passwordCorrect = Hash.Validate(LoginRequest.Password, maybeUser.HashSalt, maybeUser.HashPassword);
                if (!passwordCorrect)
                {
                    Error = "Deze combinatie email en wachtwoord is onjuist.";
                    return(Page());
                }

                // Create a JWT token and set JWTAuthToken Cookie
                var jwt        = new Jwt();
                var token      = jwt.Create(maybeUser);
                var cookieOpts = new CookieOptions {
                    MaxAge   = token.TimeSpan,
                    HttpOnly = true,
                    Secure   = true,
                    SameSite = SameSiteMode.Strict,
                };
                Response.Cookies.Append("JWTAuthToken", token.Token, cookieOpts);

                Success = "Je bent nu ingelogd!";
                return(Page());
            }
        }
예제 #6
0
        public async Task <ApiResult <dynamic> > Login([FromBody] LoginRequest request)
        {
            var rsp = new ApiResult <dynamic>();

            if (!ModelState.IsValid)
            {
                rsp.StatusCode = 400;
                rsp.Message    = "Invalid Request";
                return(rsp);
            }

            var res = await _userService.LoginAsync(request);

            if (!res.Success)
            {
                rsp.Message    = res.Message;
                rsp.StatusCode = res.StatusCode;
                return(rsp);
            }

            var info = res.Data;

            if (!PasswordHash.PasswordHash.ValidatePassword(request.Password, info.Password))
            {
                rsp.Message = "用户名或密码错误!";
                return(rsp);
            }

            var token = Jwt.Create(_tokenManagement, info);

            rsp.Message = "登录成功";
            rsp.Success = true;
            rsp.Data    = new
            {
                uuid  = info.Id,
                name  = info.RealName,
                token = "Bearer " + token
            };
            return(rsp);
        }