Exemple #1
0
        private string CreateJwtTokenForClient(AuthClientUser authModel)
        {
            try
            {
                JwtSettings settings = new JwtSettings();
                jwtModel = settings.Initiate();

                SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtModel.Key));

                List <Claim> jwtClaims = new List <Claim>();

                jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Sub, authModel.User.MobileNo.ToString()));
                jwtClaims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()));

                jwtClaims.Add(new Claim("IsAuthenticated", authModel.IsAuthenticated.ToString().ToLower()));

                var token = new JwtSecurityToken(jwtModel.Issuer, jwtModel.Audience, jwtClaims, DateTime.UtcNow, DateTime.UtcNow.AddMinutes(jwtModel.MinutesToExpiration), new SigningCredentials(key, SecurityAlgorithms.HmacSha256));

                return(new JwtSecurityTokenHandler().WriteToken(token));
            }

            catch (Exception ex)
            {
                return(ex.ToString());
            }
        }
Exemple #2
0
        public async Task <object> Login([FromBody] LogIn model)
        {
            try
            {
                using (var httpClient = new HttpClient())
                {
                    ApiInfo        apiInfo        = new ApiInfo();
                    AuthClientUser authClientUser = null;
                    using (var response = await httpClient.PostAsJsonAsync(apiInfo.Ip + apiInfo.SecurityApiServer + "/Security/ClientLogIn", model))
                    {
                        authClientUser = await response.Content.ReadAsAsync <AuthClientUser>();

                        if (authClientUser.IsAuthenticated)
                        {
                            authClientUser.BearerToken = CreateJwtTokenForClient(authClientUser);
                        }

                        return(StatusCode(StatusCodes.Status200OK, authClientUser));
                    }
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status401Unauthorized, ex.ToString()));
            }
        }
Exemple #3
0
        public object ClientLogIn([FromBody] LoginModel model)
        {
            try
            {
                AuthClientUser obj = merchantUserService.ClientLogIn(model);
                if (obj.IsAuthenticated && obj.User.LogInStatus == "N")
                {
                    obj.IsAuthenticated = false;
                }
                if (obj.IsAuthenticated && obj.User.Pstatus == "L")
                {
                    obj.IsAuthenticated = false;
                }

                if (obj.IsAuthenticated)
                {
                    //obj.BearerToken = CreateJwtTokenForClient(obj);
                    //return StatusCode(StatusCodes.Status200OK, obj);
                    return(obj);
                }
                else
                {
                    return(StatusCode(StatusCodes.Status200OK, obj));
                }
            }
            catch (Exception ex)
            {
                errorLogService.InsertToErrorLog(ex, MethodBase.GetCurrentMethod().Name, Request.Headers["UserInfo"].ToString());
                return(StatusCode(StatusCodes.Status401Unauthorized));;
            }
        }
Exemple #4
0
        private AuthClientUser BuildAuthClientUser(MerchantUser model)
        {
            AuthClientUser AuthClientUser = new AuthClientUser();

            AuthClientUser.User = model;
            //AuthClientUser.User.Mtype = model.Mtype;
            if (AuthClientUser.User.Is_validated)
            {
                AuthClientUser.IsAuthenticated = true;
                AuthClientUser.BearerToken     = Guid.NewGuid().ToString();
            }
            else
            {
                AuthClientUser.IsAuthenticated = false;
            }

            return(AuthClientUser);
        }
Exemple #5
0
        public async Task <object> Login([FromBody] LogIn model)
        {
            try
            {
                using (var httpClient = new HttpClient())
                {
                    ApiInfo        apiInfo        = new ApiInfo();
                    AuthClientUser authClientUser = null;
                    //httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJva3dhbGxldCIsImp0aSI6IjdmNmJjZWRiLTk1OGYtNDExZS1hM2IzLTQ1MjllYTNmZDk1NSIsIklzQXV0aGVudGljYXRlZCI6InRydWUiLCJuYmYiOjE1OTIzNzY2MzksImV4cCI6MTU5MjM5NDYzOSwiYXVkIjoiTWZzQXVkaWVuY2UifQ.mD7FSYZkY-CN5VCUxUQE6KliRVWVr0TEbgxALJQNLbA");
                    //httpClient.DefaultRequestHeaders.Add("ApiKey", "okwallet");
                    using (var response = await httpClient.PostAsJsonAsync(apiInfo.Ip + apiInfo.SecurityApiServer + "/Security/ClientLogIn", model))
                    {
                        authClientUser = await response.Content.ReadAsAsync <AuthClientUser>();

                        authClientUser.BearerToken = CreateJwtTokenForClient(authClientUser);
                        return(StatusCode(StatusCodes.Status200OK, authClientUser));
                    }
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status401Unauthorized));
            }
        }