Esempio n. 1
0
        //[TestMethod]
        public void CreateUserMysql()
        {
            Random randomNumber = new Random();
            string firstName    = "UnitTestOne" + randomNumber.Next(1000000);
            string secondName   = "UnitTestMYSQL" + randomNumber.Next(1000000);

            UserTable user = new UserTable()
            {
                FirstName = firstName,
                Surname   = secondName,
                Dob       = DateTime.Now,
                Gender    = UserDataConstants.Male,
                UserName  = firstName + "_" + secondName,
                Password  = "******" + firstName + "_" + secondName
            };

            AdvancedUser advancedUser = new AdvancedUser()
            {
                FirstName            = "advance " + firstName,
                Surname              = "advance " + secondName,
                Dob                  = DateTime.Now,
                Gender               = UserDataConstants.Male,
                UserName             = "******" + firstName + "_" + secondName,
                Password             = "******" + firstName + "_" + secondName,
                AdvanceStartDatetime = DateTime.Now,
                AdvanceEndDatetime   = DateTime.Now.AddDays(50)
            };

            UserPersistant createUser = new UserPersistant(null, DbContextConstant.MySqlDbContext);

            createUser.AddToPending(user);
            createUser.AddToPending(advancedUser);
            createUser.SaveChange();
        }
        public static IEnumerable <AdvancedUser> CreateAdvancedUsers()
        {
            var list             = new List <AdvancedUser>();
            var assy             = typeof(User).Assembly;
            var instanciateAttrs = (InstantiateAdvancedUserAttribute[])Attribute.GetCustomAttributes(assy, typeof(InstantiateAdvancedUserAttribute));

            Type[] t         = { typeof(int), typeof(int) };
            var    matchAttr = (MatchParameterWithPropertyAttribute[])typeof(AdvancedUser).GetConstructor(t).GetCustomAttributes(typeof(MatchParameterWithPropertyAttribute), false);

            var defValAttrId   = (DefaultValueAttribute)typeof(AdvancedUser).GetProperty(matchAttr.First(a => a.ParameterName == "id").PropertyName).GetCustomAttribute(typeof(DefaultValueAttribute));
            var defaultValueId = (int)defValAttrId.Value;

            var defValAttrExtId        = (DefaultValueAttribute)typeof(AdvancedUser).GetProperty(matchAttr.First(a => a.ParameterName == "externalId").PropertyName).GetCustomAttribute(typeof(DefaultValueAttribute));
            var defaultValueExternalId = (int)defValAttrExtId.Value;

            foreach (var attr in instanciateAttrs)
            {
                int id         = ReferenceEquals(attr.Id, null) ? defaultValueId : attr.Id.Value;
                int externalId = ReferenceEquals(attr.ExternalId, null) ? defaultValueExternalId : attr.ExternalId.Value;

                var user = new AdvancedUser(id, externalId)
                {
                    FirstName = attr.FirstName, LastName = attr.LastName
                };
                list.Add(user);
            }

            return(list);
        }
 private User ConstructUser(INewUserDataContract userContract)
 {
     if (userContract is NewAdvancedUserDataContract)
     {
         AdvancedUser advancedUser = new AdvancedUser();
         advancedUser.UserName             = userContract.UserName;
         advancedUser.Password             = SecurityHashWrapper.Get512HashString(userContract.Password);
         advancedUser.EmailAddress         = userContract.Password;
         advancedUser.FirstName            = userContract.FirstName;
         advancedUser.Surname              = userContract.Surname;
         advancedUser.DOB                  = userContract.Dob;
         advancedUser.Gender               = userContract.Gender;
         advancedUser.AdvanceStartDatetime = DateTime.Now;
         advancedUser.AdvanceEndDatetime   = DateTime.Now.AddYears(1);
         return(advancedUser);
     }
     else
     {
         User user = new User();
         user.UserName     = userContract.UserName;
         user.Password     = SecurityHashWrapper.Get512HashString(userContract.Password);
         user.EmailAddress = userContract.Password;
         user.FirstName    = userContract.FirstName;
         user.Surname      = userContract.Surname;
         user.DOB          = userContract.Dob;
         user.Gender       = userContract.Gender;
         return(user);
     }
 }
Esempio n. 4
0
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += _BackRequested;
            data = (UserAccessToken)e.Parameter;
            await data.Init();

            var imageUrl = "https://twitter.com/" + data.screenName + "/profile_image?size=original";

            data.user.profile_image_url_https = imageUrl;
            viewModel            = new AdvancedUser(data);
            mainGrid.DataContext = viewModel;
        }
        private async Task LoadAsync(AdvancedUser user)
        {
            var userName = await _userManager.GetUserNameAsync(user);

            var phoneNumber = await _userManager.GetPhoneNumberAsync(user);

            Username = userName;

            Input = new InputModel
            {
                PhoneNumber = phoneNumber
            };
        }
Esempio n. 6
0
        private async Task LoadAsync(AdvancedUser user)
        {
            var email = await _userManager.GetEmailAsync(user);

            Email = email;

            Input = new InputModel
            {
                NewEmail = email,
            };

            IsEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user);
        }
Esempio n. 7
0
 private AdvancedUserInfoContract ConvertAdvancedUserToContract(AdvancedUser advancedUser)
 {
     return(new AdvancedUserInfoContract
     {
         UserName = advancedUser.UserName,
         EmailAddress = advancedUser.EmailAddress,
         FirstName = advancedUser.FirstName,
         Surname = advancedUser.Surname,
         Dob = advancedUser.DOB,
         Gender = advancedUser.Gender,
         AdvanceStartDatetime = advancedUser.AdvanceStartDatetime,
         AdvanceEndDatetime = advancedUser.AdvanceEndDatetime
     });
 }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new AdvancedUser {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
        private static User CreateUser(InstantiateUserAttribute userAttribute, Type userType)
        {
            User user;

            if (userType == typeof(User))
            {
                user = new User(userAttribute.Id ?? MatchPataremeter("id", userType));
            }
            else
            {
                var advancedUserAttribute = userAttribute as InstantiateAdvancedUserAttribute;
                user =
                    new AdvancedUser(advancedUserAttribute.Id ?? MatchPataremeter("id", userType),
                                     advancedUserAttribute.ExternalId ?? MatchPataremeter("externalId", userType));
            }
            InitializeUser(user, userAttribute);
            return(user);
        }
Esempio n. 10
0
        private async Task LoadSharedKeyAndQrCodeUriAsync(AdvancedUser user)
        {
            // Load the authenticator key & QR code URI to display on the form
            var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);

            if (string.IsNullOrEmpty(unformattedKey))
            {
                await _userManager.ResetAuthenticatorKeyAsync(user);

                unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
            }

            SharedKey = FormatKey(unformattedKey);

            var email = await _userManager.GetEmailAsync(user);

            AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey);
        }
Esempio n. 11
0
 public void CreateNewAdvancedUser(AdvancedUser user)
 {
     try
     {
         if (user.UserName != null && user.UserName != "" && UsernameAlreadyExist(user.UserName))
         {
             throw new InvalidOperationException("This username has already been taken.");
         }
         PersistNewUser(user);
     }
     catch (InvalidOperationException exception)
     {
         ThrowUserExistErrorMessage(exception.Message);
     }
     catch (Exception exception)
     {
         ThrowErrorMessage(exception.Message);
     }
 }
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information during confirmation.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            if (ModelState.IsValid)
            {
                var user = new AdvancedUser {
                    UserName = Input.Email, Email = Input.Email
                };

                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);

                        var userId = await _userManager.GetUserIdAsync(user);

                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                        var callbackUrl = Url.Page(
                            "/Account/ConfirmEmail",
                            pageHandler: null,
                            values: new { area = "Identity", userId = userId, code = code },
                            protocol: Request.Scheme);

                        await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                          $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                        // If account confirmation is required, we need to show the link if we don't have a real email sender
                        if (_userManager.Options.SignIn.RequireConfirmedAccount)
                        {
                            return(RedirectToPage("./RegisterConfirmation", new { Email = Input.Email }));
                        }

                        await _signInManager.SignInAsync(user, isPersistent : false, info.LoginProvider);

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            ProviderDisplayName = info.ProviderDisplayName;
            ReturnUrl           = returnUrl;
            return(Page());
        }