Exemplo n.º 1
0
        public IActionResult OnGet()
        {
            var id            = _UserManager.GetUserId(User);
            var get_user_list = _UserManager.Users.ToList();
            var adminId       = get_user_list[0].Id;

            if (!_appDbContext.RoleAdmins.Any())
            {
                var role = new RoleAdmin()
                {
                    userId = adminId,
                    role   = "admin"
                };
                _appDbContext.Add(role);
                _appDbContext.SaveChanges();
            }

            var getAdmin = from a in _appDbContext.RoleAdmins select a;

            foreach (var item in getAdmin)
            {
                if (id == item.userId)
                {
                    return(new RedirectToPageResult("AdminHome"));
                }
            }
            // if(id == adminId) {
            //     return new RedirectToPageResult("Admin");
            // }
            return(new RedirectToPageResult("User"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            if (!_roleManager.RoleExistsAsync("Administrator").Result)
            {
                RoleAdmin newRole = new RoleAdmin("Administrator", "Administrators can do something with data");
                await _roleManager.CreateAsync(newRole);
            }



            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                if (true)//model.Email.Contains("@lioba.de")&&model.Email.Length==13)
                {
                    ViewData["Emailok"] = "true";
                    var user = new Teacher {
                        Nachname = model.Nachname, Email = model.Email, Vorname = model.Vorname, Anrede = model.Anrede
                    };
                    user.UserName = user.Id;
                    var result = await _userManager.CreateAsync(user, model.Password);

                    if (result.Succeeded)
                    {
                        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713

                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code },
                                                     protocol: HttpContext.Request.Scheme);
                        await _emailSender.SendEmailAsync(user.Vorname + " " + user.Nachname, model.Email, "Accountbestätigung Raumplanung",
                                                          $"Bitte bestätige deinen Account, indem du diesen Link klickst: <a href='{callbackUrl}'>link</a>");

                        if (model.Admin)
                        {
                            await _userManager.AddToRoleAsync(user, "Administrator");
                        }
                        //await _signInManager.SignInAsync(user, isPersistent: false);
                        _logger.LogInformation(3, "User created a new account with password.");
                        return(RedirectToAction("Waitforemailconfirm", "Account"));
                    }
                    AddErrors(result);
                }
                else
                {
                    ViewData["Emailok"] = "false";
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Exemplo n.º 3
0
        public void OnPost(string id, string role)
        {
            Console.WriteLine(id);
            Console.WriteLine(role);
            if (role == "admin")
            {
                var get_id    = id.Replace("set-", "");
                var is_it_new = from a in _appDbContext.RoleAdmins where get_id == a.userId select a;
                if (!is_it_new.Any())
                {
                    var Admin = new RoleAdmin {
                        role   = "admin",
                        userId = get_id
                    };
                    _appDbContext.Add(Admin);
                    _appDbContext.SaveChanges();
                }
            }

            else if (role == "user")
            {
                var get_id    = id.Replace("set-", "");
                var is_it_new = from a in _appDbContext.RoleAdmins where get_id == a.userId select a;
                if (is_it_new != null)
                {
                    foreach (var a in is_it_new)
                    {
                        _appDbContext.Remove(a);
                    }
                    _appDbContext.SaveChanges();
                }
            }
            var get_user_list = _UserManager.Users.ToList();

            ViewData["Data_user"] = get_user_list;
            var get_admin = from a in _appDbContext.RoleAdmins select a;

            ViewData["Admin"] = get_admin;
        }