Exemple #1
0
        public AuthenticateResult Authenticate(LoginModel logonModel)
        {
            if (logonModel != null)
            {
                User user = null;
                if (!string.IsNullOrEmpty(logonModel.Token))
                {
                    string tokenText = RSAEncryption.Decrypt(logonModel.Token);

                    ApiToken token;
                    if (ApiToken.TryParse(tokenText, out token))
                    {
                        if (token.AuthenticationTime > DateTime.Today.AddDays(-10))
                        {
                            user = _userRepository.GetUser(token.UserId);
                        }
                    }
                }
                else
                {
                    user = _userRepository.GetUser(logonModel.Username);
                    if (user != null)
                    {
                        if (user.Password != logonModel.Password)
                        {
                            return(new AuthenticateResult(false, null)
                            {
                                Message = "invalid password"
                            });
                        }
                    }
                }

                if (user != null)
                {
                    ApiToken token = new ApiToken()
                    {
                        UserId   = user.Id,
                        UserName = user.Username,
                        Timezone = user.Timezone,
                        Language = user.Language,
                        //Roles = SerializeRoles(user.StaffAtBusinesses),
                        AuthenticationTime = DateTime.Now
                    };

                    string encryptedToken = RSAEncryption.Encrypt(token.ToString());
                    user.Password = null; // clear password
                    AuthenticateResult result = new AuthenticateResult(true, encryptedToken)
                    {
                        User = new UserDTO(user)
                    };

                    return(result);
                }
            }

            return(new AuthenticateResult(false, null)
            {
                Message = "invalid login"
            });
        }
        public async Task <string> PerformLoginActions(string username, string password)
        {
            try
            {
                bool           success = false;
                HttpStatusCode code;
                string         responseBody = "";
                string         ApiToken;
                using (Httpclient = new HttpClient())
                {
                    Httpclient.DefaultRequestHeaders.Clear();
                    Httpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var values = new List <KeyValuePair <string, string> >
                    {
                        new KeyValuePair <string, string>("grant_type", "password"),
                        new KeyValuePair <string, string>("username", username),
                        new KeyValuePair <string, string> ("Password", password)
                    };

                    FormUrlEncodedContent postBody = new FormUrlEncodedContent(values);
                    Task taskDownload = Httpclient.PostAsync((myserviceurl()), postBody)
                                        .ContinueWith(task =>
                    {
                        if (task.Status == TaskStatus.RanToCompletion)
                        {
                            var response = task.Result;

                            if (response.IsSuccessStatusCode)
                            {
                                success          = true;
                                code             = response.StatusCode;
                                responseBody     = response.Content.ReadAsStringAsync().Result;
                                ApiToken         = responseBody;
                                TokenGrant token = JsonConvert.DeserializeObject <TokenGrant>
                                                       (ApiToken.ToString());
                                ApiToken      = token.access_token;
                                DataValues md = TokenDecode.GetInstance()
                                                .Decode(token);
                                md.UserName = username;
                                ApplicationsVariables.Username   = username;
                                ApplicationsVariables.Token      = md.Token;
                                ApplicationsVariables.Datavalues = md;
                            }
                            else
                            {
                                code         = response.StatusCode;
                                responseBody = response.Content.ReadAsStringAsync().Result;
                            }
                        }
                    });
                    taskDownload.Wait();
                }
                if (success)
                {
                    responseBody = "ok";
                }
                return(responseBody);

                //if (success)
                //    return "ok";
                //else if (responseBody.Contains("invalid_grant"))
                //    return responseBody;
                //else if (responseBody.Contains("no_confirmed"))
                //    return responseBody;
                throw new Exception();
            }
            catch (Exception ex)
            {
                throw;
            }
        }