예제 #1
0
        public IHttpActionResult Login(String userName, String userPassword)
        {
            try
            {
                // megkeressük a felhasználót
                IdentityGuest user = _userManager.Find(userName, userPassword);

                if (user == null) // ha nem sikerült, akkor nincs bejelentkeztetés
                {
                    return(NotFound());
                }

                // ha valaki már bejelentkezett, kijelentkeztetjük
                HttpContext.Current.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

                // bejelentkeztetjük az új felhasználót
                ClaimsIdentity claimsIdentity = _userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                HttpContext.Current.GetOwinContext().Authentication.SignIn(new AuthenticationProperties {
                    IsPersistent = false
                }, claimsIdentity);

                return(Ok());
            }
            catch
            {
                return(NotFound());
            }
        }
예제 #2
0
        protected override void Seed(AdministratorDbContext context)
        {
            // adatbázis feltöltése adatokkal

            if (!context.Users.Any(u => u.UserName == "admin"))
            {
                // ammenyiben még nincs admin felhasználó
                RoleManager <IdentityRole>  roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
                UserManager <IdentityGuest> userManager = new UserManager <IdentityGuest>(new UserStore <IdentityGuest>(context));

                IdentityRole role = new IdentityRole {
                    Name = "administrator"
                };                                                               // létrehozzuk az administrator csoportot
                roleManager.Create(role);

                IdentityGuest adminUser = new IdentityGuest {
                    UserName = "******"
                };                                                                  // létrehozzuk az admin felhasználót a csoportban
                userManager.Create(adminUser, "password");
                userManager.AddToRole(adminUser.Id, "administrator");
            }
        }