Beispiel #1
0
        internal static void Initialize(DbContext context, Microsoft.AspNetCore.Identity.RoleManager <Microsoft.AspNetCore.Identity.IdentityRole> roleManager, Microsoft.AspNetCore.Identity.UserManager <Microsoft.AspNetCore.Identity.IdentityUser> userManager)
        {
            if (!roleManager.RoleExistsAsync("Admin").Result)
            {
                IdentityRole role = new IdentityRole("Admin");

                roleManager.CreateAsync(role).Wait();
            }

            if (!roleManager.RoleExistsAsync("NormalUser").Result)
            {
                IdentityRole role = new IdentityRole("NormalUser");

                roleManager.CreateAsync(role).Wait();
            }

            //------------------------------------------------------------------------------------//

            if (userManager.FindByNameAsync("Abduallah").Result == null)
            {
                IdentityUser user = new IdentityUser();
                user.Email    = "*****@*****.**";
                user.UserName = "******";

                IdentityResult result = userManager.CreateAsync(user, "Abduallah1996-").Result;

                if (result.Succeeded)
                {
                    userManager.AddToRoleAsync(user, "Admin").Wait();
                }
            }

            if (userManager.FindByNameAsync("Abdullah").Result == null)
            {
                IdentityUser user = new IdentityUser();
                user.Email    = "*****@*****.**";
                user.UserName = "******";


                var result = userManager.CreateAsync(user, "Abduallah1996-");

                if (result.IsCompletedSuccessfully)
                {
                    userManager.AddToRoleAsync(user, "NormalUser").Wait();
                }
            }
            //------------------------------------------------------------------------------------------

            //Glöm inte att spara
            //context.SaveChanges();
        }
Beispiel #2
0
        internal static void Initialize(AppDbContext context, Microsoft.AspNetCore.Identity.RoleManager <Microsoft.AspNetCore.Identity.IdentityRole> roleManager, Microsoft.AspNetCore.Identity.UserManager <Microsoft.AspNetCore.Identity.IdentityUser> userManager)
        {
            if (!roleManager.RoleExistsAsync("Admin").Result)
            {
                IdentityRole role = new IdentityRole("Admin");

                roleManager.CreateAsync(role).Wait();
            }

            if (!roleManager.RoleExistsAsync("NormalUser").Result)
            {
                IdentityRole role = new IdentityRole("NormalUser");

                roleManager.CreateAsync(role).Wait();
            }

            //-------------------------------------------------------------------

            if (userManager.FindByNameAsync("Guru").Result == null)
            {
                IdentityUser user = new IdentityUser();
                user.Email    = "*****@*****.**";
                user.UserName = "******";

                IdentityResult result = userManager.CreateAsync(user, "Password!123").Result;

                if (result.Succeeded)
                {
                    userManager.AddToRoleAsync(user, "Admin").Wait();
                }
            }

            if (userManager.FindByNameAsync("Sven").Result == null)
            {
                IdentityUser user = new IdentityUser();
                user.Email    = "*****@*****.**";
                user.UserName = "******";

                IdentityResult result = userManager.CreateAsync(user, "Password!123").Result;

                if (result.Succeeded)
                {
                    userManager.AddToRoleAsync(user, "NormalUser").Wait();
                }
            }

            //-----------------------------------------------------------------------

            // Don´t forget to save
            //context.SaveChanges();
        }
Beispiel #3
0
        public IActionResult Index()
        {
            //Init applicationuser so i can acces values to show on index page.
            ApplicationUser user = _userManager.FindByNameAsync(User.Identity.Name).Result;

            return(View(user));
        }
        public async Task <IActionResult> Login([FromBody] LoginModel loginModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var user = await userManager.FindByNameAsync(loginModel.Email);

            if (!(user != null && await userManager.CheckPasswordAsync(user, loginModel.Password)))
            {
                return(Unauthorized());
            }
            if (!await userManager.IsEmailConfirmedAsync(user))
            {
                return(Ok(new { success = false, message = "Check your email to verify your account." }));
            }
            return(Ok(new { access_token = new ApplicationJwtProvider(Configuration, userManager).JwtTokenBuilder(user).Result }));
        }
Beispiel #5
0
        public static async Task <bool> HasAccess(this ClaimsPrincipal User, Microsoft.AspNetCore.Identity.UserManager <ApplicationUser> userManager, string access)
        {
            if (User.IsInRole("Admin"))
            {
                return(true);
            }
            var user = await userManager.FindByNameAsync(User.Identity.Name);

            var claims = await userManager.GetClaimsAsync(user);

            return(claims.Any(x => x.Value == access));
        }
Beispiel #6
0
        public async Task <IActionResult> UpdateGuestCustomerinfo([FromBody] EditGuestUser value)
        {
            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            var appuser = new AppUser()
            {
                UserName    = value.Name,
                Email       = value.Email,
                PhoneNumber = value.PhoneNo
            };

            if (user != null)
            {
                await _userManager.UpdateAsync(appuser);

                return(Ok());
            }
            return(NotFound());
        }
        public async System.Threading.Tasks.Task <Microsoft.AspNetCore.Mvc.IActionResult> Login(Models.ViewModels.LoginModel accountViewModel)
        {
            if (ModelState.IsValid)
            {
                Microsoft.AspNetCore.Identity.IdentityUser user = await userManager.FindByNameAsync(accountViewModel.Name);

                if (user != null)
                {
                    await signInManager.SignOutAsync();

                    if ((await signInManager.PasswordSignInAsync(user, accountViewModel.Password, false, false)).Succeeded)
                    {
                        return(Redirect(accountViewModel?.ReturnUrl ?? "/Admin/Index"));
                    }
                }
            }
            ModelState.AddModelError("", "Invalid name or password");
            return(View(accountViewModel));
        }
Beispiel #8
0
        protected override async Task <Response <CreateResult> > HandleCore(LoginRequest request)
        {
            var user = await _userManager.FindByNameAsync(request.UserName);

            if (user == null)
            {
                return(new Response <CreateResult>(CreateResult.NotCreated));
            }
            var passwordCheckResult = await _userManager.CheckPasswordAsync(user, request.Password);

            // todo: add new results with errors, (password bad, user not found, user locked and etc.)
            if (passwordCheckResult)
            {
                return(new Response <CreateResult>(CreateResult.Created));
            }
            else
            {
                return(new Response <CreateResult>(CreateResult.NotCreated));
            }
        }