Пример #1
0
        public async Task<IActionResult> OnPostAsync(string returnUrl = null)
        {
            ReturnUrl = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new Stockholder { 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);
                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                    await _emailSender.SendEmailConfirmationAsync(Input.Email, callbackUrl);

                    await _signInManager.SignInAsync(user, isPersistent: false);
                    return LocalRedirect(Url.GetLocalUrl(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return Page();
        }
Пример #2
0
        public async Task <IActionResult> OnPostAsync()
        {
            var user = new Stockholder()
            {
                UserName = Input.Email, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName, Address = Input.Address, ZipCode = Input.Zipcode, City = Input.City, PhoneNumber = Input.PhoneNumber
            };
            var result = await users.CreateAsync(user);

            if (result.Succeeded)
            {
                await users.AddToRoleAsync(user, "societaire");
            }
            return(RedirectToPage("Index"));
        }
        private async Task LoadSharedKeyAndQrCodeUriAsync(Stockholder 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);
            AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey);
        }
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await _signInManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    throw new ApplicationException("Error loading external login information during confirmation.");
                }
                var user = new Stockholder {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user);

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

                    if (result.Succeeded)
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                        return(LocalRedirect(Url.GetLocalUrl(returnUrl)));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            ReturnUrl = returnUrl;
            return(Page());
        }
Пример #5
0
        internal async Task SeedAsync(ApplicationDbContext context, IHostingEnvironment env, Microsoft.Extensions.Options.IOptions <AdminSettings> settings, UserManager <Stockholder> userManager, RoleManager <IdentityRole> roleManager)
        {
            using (context)
            {
                context.Database.Migrate();

                try
                {
                    if (!roleManager.Roles.Any())
                    {
                        var result = await roleManager.CreateAsync(new IdentityRole("gerant") { NormalizedName = "GERANT" });

                        await roleManager.CreateAsync(new IdentityRole("societaire") { NormalizedName = "SOCIETAIRE" });
                    }

                    var adminRole = roleManager.Roles.First(f => f.Name == "gerant");
                    var adminUser = await userManager.GetUsersInRoleAsync(adminRole.Name);

                    if (!adminUser.Any())
                    {
                        var user = new Stockholder {
                            FirstName = settings.Value.FirstName, LastName = settings.Value.LastName, UserName = settings.Value.Email, Email = settings.Value.Email
                        };
                        var userResult = await userManager.CreateAsync(user, settings.Value.Password);

                        if (userResult.Succeeded)
                        {
                            await userManager.AddToRoleAsync(user, adminRole.Name);
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        public void Example()
        {
            #region Usage
            Stockholder stockholder = new Stockholder
            {
                FullName   = "Steve Stockholder",
                Businesses = new List <Business>
                {
                    new Hotel
                    {
                        Name  = "Hudson Hotel",
                        Stars = 4
                    }
                }
            };

            string jsonTypeNameAll = JsonConvert.SerializeObject(stockholder, Formatting.Indented, new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All
            });

            Console.WriteLine(jsonTypeNameAll);
            // {
            //   "$type": "BESSy.Json.Samples.Stockholder, BESSy.Json.Tests",
            //   "FullName": "Steve Stockholder",
            //   "Businesses": {
            //     "$type": "System.Collections.Generic.List`1[[BESSy.Json.Samples.Business, BESSy.Json.Tests]], mscorlib",
            //     "$values": [
            //       {
            //         "$type": "BESSy.Json.Samples.Hotel, BESSy.Json.Tests",
            //         "Stars": 4,
            //         "Name": "Hudson Hotel"
            //       }
            //     ]
            //   }
            // }

            string jsonTypeNameAuto = JsonConvert.SerializeObject(stockholder, Formatting.Indented, new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.Auto
            });

            Console.WriteLine(jsonTypeNameAuto);
            // {
            //   "FullName": "Steve Stockholder",
            //   "Businesses": [
            //     {
            //       "$type": "BESSy.Json.Samples.Hotel, BESSy.Json.Tests",
            //       "Stars": 4,
            //       "Name": "Hudson Hotel"
            //     }
            //   ]
            // }

            // for security TypeNameHandling is required when deserializing
            Stockholder newStockholder = JsonConvert.DeserializeObject <Stockholder>(jsonTypeNameAuto, new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.Auto
            });

            Console.WriteLine(newStockholder.Businesses[0].GetType().Name);
            // Hotel
            #endregion
        }
Пример #7
0
        public void Example()
        {
            #region Usage
            Stockholder stockholder = new Stockholder
            {
                FullName = "Steve Stockholder",
                Businesses = new List<Business>
                {
                    new Hotel
                    {
                        Name = "Hudson Hotel",
                        Stars = 4
                    }
                }
            };

            string jsonTypeNameAll = JsonConvert.SerializeObject(stockholder, Formatting.Indented, new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All
            });

            Console.WriteLine(jsonTypeNameAll);
            // {
            //   "$type": "Newtonsoft.Json.Samples.Stockholder, Newtonsoft.Json.Tests",
            //   "FullName": "Steve Stockholder",
            //   "Businesses": {
            //     "$type": "System.Collections.Generic.List`1[[Newtonsoft.Json.Samples.Business, Newtonsoft.Json.Tests]], mscorlib",
            //     "$values": [
            //       {
            //         "$type": "Newtonsoft.Json.Samples.Hotel, Newtonsoft.Json.Tests",
            //         "Stars": 4,
            //         "Name": "Hudson Hotel"
            //       }
            //     ]
            //   }
            // }

            string jsonTypeNameAuto = JsonConvert.SerializeObject(stockholder, Formatting.Indented, new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.Auto
            });

            Console.WriteLine(jsonTypeNameAuto);
            // {
            //   "FullName": "Steve Stockholder",
            //   "Businesses": [
            //     {
            //       "$type": "Newtonsoft.Json.Samples.Hotel, Newtonsoft.Json.Tests",
            //       "Stars": 4,
            //       "Name": "Hudson Hotel"
            //     }
            //   ]
            // }

            // for security TypeNameHandling is required when deserializing
            Stockholder newStockholder = JsonConvert.DeserializeObject<Stockholder>(jsonTypeNameAuto, new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.Auto
            });

            Console.WriteLine(newStockholder.Businesses[0].GetType().Name);
            // Hotel
            #endregion

            Assert.AreEqual("Hotel", newStockholder.Businesses[0].GetType().Name);
        }