public GenericActionResult<string> CreateAuthorizationToken(string userName, string password)
        {
            var authenticateUserResult = _authenticationManagement.AuthenticateUser(userName, password);

            if (!authenticateUserResult.IsSuccess)
                return new GenericActionResult<string>() { IsSuccess = false, Result = null, Errors = authenticateUserResult.Errors };

            //get all information on person,roles & claims
            var user = _userManagement.FindByUserName(userName);

            var userAuthorizationInfo = new UserAuthorizationInfo()
            {
                UserName = authenticateUserResult.Result.UserName,
                ExpiryDate = DateTime.Now.AddMinutes(Convert.ToInt32(ConfigurationManager.AppSettings["tokenExpirationInMinutes"])),
                UserId = user.Id,
                Roles = user.Roles
            };

            //serialize object for storing in database
            var serializedAuthorizationInfo = JsonConvert.SerializeObject(userAuthorizationInfo);

            var authToken = _dataAccess.CreateToken(serializedAuthorizationInfo);

            var authenticationResult = new GenericActionResult<string>() { Errors = null, IsSuccess = true, Result = authToken };

            return authenticationResult;
        }
 public GenericActionResult <UserCreditResponseModel> GetUserCreditById(int id)
 {
     try
     {
         return(GenericActionResult <UserCreditResponseModel> .FromObject(context.UserCredits.Where(a => a.Id == id && !a.IsDeleted).Select(a => new ObjectConverterManager(context, userManager).ToUserCreditResponseModel(a)).FirstOrDefault()));
     }
     catch (Exception) { return(new GenericActionResult <UserCreditResponseModel>("Failed to get credit, please try again or contact the administrator.")); }
 }
 public GenericActionResult <List <UserCreditResponseModel> > GetUserCreditByUserId(string userId)
 {
     try
     {
         var credits = context.UserCredits.Where(a => a.UserId.Equals(userId) && !a.IsDeleted).ToList();
         return(GenericActionResult <List <UserCreditResponseModel> > .FromObject(credits.Select(a => new ObjectConverterManager(context, userManager).ToUserCreditResponseModel(a)).ToList()));
     }
     catch (Exception) { return(new GenericActionResult <List <UserCreditResponseModel> >("Failed to get credits, please try again or contact the administrator.")); }
 }
 public GenericActionResult <List <UserCreditResponseModel> > GetAllUserCredits(int skip, int take)
 {
     try
     {
         var credits = context.UserCredits.Where(a => !a.IsDeleted).Skip(skip).Take(take).ToList();
         return(GenericActionResult <List <UserCreditResponseModel> > .FromObject(credits.Select(
                                                                                      a => new ObjectConverterManager(context, userManager).ToUserCreditResponseModel(a)).ToList()));
     }
     catch (Exception) { return(new GenericActionResult <List <UserCreditResponseModel> >("Failed to get credits, please try again or contact the administrator.")); }
 }
Exemple #5
0
        public GenericActionResult <UserDataModel> AuthenticateUser(string userName, string password)
        {
            var identityResult = this.Find(userName, password);

            UserDataModel userDataModel = null;

            if (identityResult != null)
            {
                if (identityResult.EmailConfirmed)
                {
                    userDataModel = _modelFactory.CreateUserDataModel(identityResult);

                    userDataModel.Roles =
                        _roleManager.GetRoleNames(identityResult.Roles.Select(c => c.RoleId).ToList()).ToList();

                    userDataModel.IsAuthenticated = true;

                    var authenticationResult = new GenericActionResult <UserDataModel>
                    {
                        Result    = userDataModel,
                        IsSuccess = true
                    };
                    return(authenticationResult);
                }
                else
                {
                    userDataModel = new UserDataModel {
                        IsAuthenticated = false
                    };
                    return(new GenericActionResult <UserDataModel>
                    {
                        Result = userDataModel,
                        IsSuccess = false,
                        Errors = new List <string> {
                            "Email has not been confirmed!"
                        }
                    });
                }
            }
            else
            {
                userDataModel = new UserDataModel {
                    IsAuthenticated = false
                };
                return(new GenericActionResult <UserDataModel>
                {
                    Result = userDataModel,
                    IsSuccess = false,
                    Errors = new List <string> {
                        "Incorrect username or password!"
                    }
                });
            }
        }
Exemple #6
0
        public GenericActionResult <string> ChangeUserPassword(string userId, string oldPassword, string newPassword)
        {
            var result = this.ChangePassword(userId, oldPassword, newPassword);

            var actionResult = new GenericActionResult <string>()
            {
                IsSuccess = result.Succeeded,
                Errors    = result.Errors
            };

            return(actionResult);
        }
        public GenericActionResult<string> ChangeUserPassword(string userId, string oldPassword, string newPassword)
        {
            var result = this.ChangePassword(userId, oldPassword, newPassword);

            var actionResult = new GenericActionResult<string>()
            {
                IsSuccess = result.Succeeded,
                Errors = result.Errors
            };

            return actionResult;
        }
        public GenericActionResult<UserDataModel> AuthenticateUser(string userName, string password)
        {
            var identityResult = this.Find(userName, password);
            UserDataModel userDataModel = null;

            if (identityResult != null)
            {
                if (identityResult.EmailConfirmed)
                {
                    userDataModel = _modelFactory.CreateUserDataModel(identityResult);
                    userDataModel.IsAuthenticated = true;

                    var authenticationResult = new GenericActionResult<UserDataModel>
                    {
                        Result = userDataModel,
                        IsSuccess = true
                    };
                    return authenticationResult;
                }
                else
                {
                    userDataModel = new UserDataModel { IsAuthenticated = false };
                    return new GenericActionResult<UserDataModel>
                    {
                        Result = userDataModel,
                        IsSuccess = false,
                        Errors = new List<string> { "Email has not been confirmed!" }
                    };
                }
            }
            else
            {
                userDataModel = new UserDataModel { IsAuthenticated = false };
                return new GenericActionResult<UserDataModel>
                {
                    Result = userDataModel,
                    IsSuccess = false,
                    Errors = new List<string> { "Incorrect username or password!" }
                };
            }
        }
 public GenericActionResult <List <UserModel> > GetAllUser()
 {
     try
     {
         var users = userManager.Users.ToListAsync().Result;
         return(GenericActionResult <List <UserModel> > .FromObject(users.Select(a => new UserModel
         {
             UserId = a.Id,
             DateCreated = a.DateCreated,
             Email = a.Email,
             UserName = a.UserName,
             PhoneNumber = a.PhoneNumber,
             IsConfirmed = a.EmailConfirmed,
             Roles = userManager.GetRolesAsync(a).Result
         }).ToList()));
     }
     catch (Exception exception)
     {
         return(new GenericActionResult <List <UserModel> >(exception.Message));
     }
 }
Exemple #10
0
        private async void OnStartStopButtonClicked(object sender, EventArgs e)
        {
            string label = ((Button)sender).Text;
            string name  = ((Button)sender).CommandParameter?.ToString();

            if (string.IsNullOrEmpty(name))
            {
                return;
            }

            if (isStartingOrStopping)
            {
                return;
            }

            isStartingOrStopping = true;

            if (await DisplayAlert(label, $"{label} system {name}?", "Go", "Cancel"))
            {
                GenericActionResult result = await vm?.DoStartStop(name);

                if (result != null)
                {
                    string prefix;

                    if (result.Success)
                    {
                        prefix = "Success";
                    }
                    else
                    {
                        prefix = "Failed";
                    }

                    await DisplayAlert($"System {label}", $"{prefix}: {result.Message}", "OK");
                }
            }

            isStartingOrStopping = false;
        }
Exemple #11
0
        public async Task <GenericActionResult> CloseAlert(string id)
        {
            try
            {
                if (string.IsNullOrEmpty(id))
                {
                    throw new ArgumentNullException(nameof(id));
                }

                CancellationTokenSource cts = new CancellationTokenSource();
                cts.CancelAfter(TimeSpan.FromMinutes(1));

                GenericActionResult result = await alertsConnector.Close(id, cts.Token);

                if (result.Success)
                {
                    HandleAlertClosure(id);
                }

                return(result);
            }
            catch (OperationCanceledException)
            {
                string err = "Not closing alert: operation cancelled";
                Debug.WriteLine(err);
                return(new GenericActionResult(false, err));
            }
            catch (ArgumentNullException ex)
            {
                string err = $"Not closing alert: missing or invalid parameter {ex.ParamName}";
                Debug.WriteLine(err);
                return(new GenericActionResult(false, err));
            }
            catch (Exception e)
            {
                string err = $"Failed to close alert: {e.Message}";
                Debug.WriteLine(err);
                return(new GenericActionResult(false, err));
            }
        }
Exemple #12
0
        public GenericActionResult <UserReturnModel> CreateUser(CreateUserBindingModel user, string password)
        {
            var newUser = new ApplicationUser()
            {
                UserName    = user.Username,
                Email       = user.Email,
                FirstName   = user.FirstName,
                LastName    = user.LastName,
                PhoneNumber = user.MobileNumber,
            };

            try
            {
                IdentityResult addUserResult = this.Create(newUser, password);

                var result = new GenericActionResult <UserReturnModel>()
                {
                    IsSuccess = addUserResult.Succeeded,
                    Errors    = addUserResult.Errors,
                    Result    = addUserResult.Succeeded ? _modelFactory.Create(newUser) : null
                };

                return(result);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Creating user failed!");

                return(new GenericActionResult <UserReturnModel>()
                {
                    Errors = new List <string>()
                    {
                        "An error occured while creating the user,please try again later."
                    },
                    IsSuccess = false,
                    Result = null
                });
            }
        }
Exemple #13
0
        public GenericActionResult <string> CreateAuthorizationToken(string userName, string password)
        {
            var authenticateUserResult = _authenticationManagement.AuthenticateUser(userName, password);

            if (!authenticateUserResult.IsSuccess)
            {
                return new GenericActionResult <string>()
                       {
                           IsSuccess = false,
                           Result    = null,
                           Errors    = authenticateUserResult.Errors
                       }
            }
            ;

            //get all information on person,roles & claims
            var user = _userManagement.FindByUserName(userName);

            var userAuthorizationInfo = new UserAuthorizationInfo()
            {
                UserName   = authenticateUserResult.Result.UserName,
                ExpiryDate = DateTime.Now.AddMinutes(Convert.ToInt32(ConfigurationManager.AppSettings["tokenExpirationInMinutes"])),
                UserId     = user.Id,
                Roles      = user.Roles
            };

            //serialize object for storing in database
            var serializedAuthorizationInfo = JsonConvert.SerializeObject(userAuthorizationInfo);

            var authToken = _dataAccess.CreateToken(serializedAuthorizationInfo);

            var authenticationResult = new GenericActionResult <string>()
            {
                Errors = null, IsSuccess = true, Result = authToken
            };

            return(authenticationResult);
        }
Exemple #14
0
        public async Task <GenericActionResult> CloseAllAlerts()
        {
            try
            {
                CancellationTokenSource cts = new CancellationTokenSource();
                cts.CancelAfter(TimeSpan.FromMinutes(1));

                GenericActionResult result = await alertsConnector.CloseAll(cts.Token);

                if (result.Success)
                {
                    foreach (var alert in openAlerts)
                    {
                        detailedAlerts.Remove(alert.AlertId);
                    }

                    alertsClosedtoday.AddRange(openAlerts);

                    openAlerts.Clear();
                }

                return(result);
            }
            catch (OperationCanceledException)
            {
                string err = "Not closing all alerts: operation cancelled";
                Debug.WriteLine(err);
                return(new GenericActionResult(false, err));
            }
            catch (Exception e)
            {
                string err = $"Failed to close all alerts: {e.Message}";
                Debug.WriteLine(err);
                return(new GenericActionResult(false, err));
            }
        }
        public GenericActionResult<UserReturnModel> CreateUser(CreateUserBindingModel user, string password)
        {
            var newUser = new ApplicationUser()
            {
                UserName = user.Email,
                Email = user.Email,
                FirstName = user.FirstName,
                LastName = user.LastName,
                PhoneNumber = user.MobileNumber
            };

            IdentityResult addUserResult = this.Create(newUser, password);

            var result = new GenericActionResult<UserReturnModel>()
            {
                IsSuccess = addUserResult.Succeeded,
                Errors = addUserResult.Errors,
                Result = addUserResult.Succeeded ? _modelFactory.Create(newUser) : null
            };

            return result;
        }