public ActionResult AddRole(AspNetRole role)
        {
            JarvisEntities context = new JarvisEntities();

            context.AspNetRoles.Add(role);
            context.SaveChanges();
            return(View());
        }
        public ActionResult AddUserToRole(string userName, string roleName)
        {
            JarvisEntities context = new JarvisEntities();
            AspNetUser     user    = context.AspNetUsers
                                     .Where(u => u.UserName == userName).FirstOrDefault();
            AspNetRole role = context.AspNetRoles
                              .Where(r => r.Name == roleName).FirstOrDefault();

            user.AspNetRoles.Add(role);
            context.SaveChanges();
            return(View());
        }
Exemple #3
0
        public IHttpActionResult Get()
        {
            JarvisEntities db = new JarvisEntities();

            var users = db.AspNetUsers.Select(a => new { Username = a.UserName, Email = a.Email }).ToList();
            JArray stoArray = (JArray)JToken.FromObject(users);
            dynamic obj = new JObject();
            obj.users = stoArray;

            if (obj == null)
            {
                return NotFound();
            }
            return Ok(obj);
        }
Exemple #4
0
        public IHttpActionResult Get()
        {
            JarvisEntities db = new JarvisEntities(); 
            var providers = db.ProviderAccounts.Select(p => new { username = p.Account.AspNetUser.UserName, p.provider }).ToList();

            JArray stoArray = (JArray)JToken.FromObject(providers);
            dynamic obj = new JObject();
            obj.providerInfo = stoArray;

            if (obj == null)
            {
                return NotFound();
            }
            return Ok(obj);
        }
        public ActionResult Login(Login login)
        {
            UserStore <IdentityUser>   userStore = new UserStore <IdentityUser>();
            UserManager <IdentityUser> manager   = new UserManager <IdentityUser>(userStore);
            IdentityUser identityUser            = manager.Find(login.UserName,
                                                                login.Password);

            if (ModelState.IsValid)
            {
                if (ValidLogin(login))
                {
                    IAuthenticationManager authenticationManager
                        = HttpContext.GetOwinContext().Authentication;
                    authenticationManager
                    .SignOut(DefaultAuthenticationTypes.ExternalCookie);

                    var identity = new ClaimsIdentity(new[] {
                        new Claim(ClaimTypes.Name, login.UserName),
                    },
                                                      DefaultAuthenticationTypes.ApplicationCookie,
                                                      ClaimTypes.Name, ClaimTypes.Role);

                    authenticationManager.SignIn(new AuthenticationProperties
                    {
                        IsPersistent = false
                    }, identity);
                    System.Threading.Thread.Sleep(2000);


                    JarvisEntities context = new JarvisEntities();
                    var            query   = context.AspNetUsers.Where(u => u.Id == identityUser.Id).FirstOrDefault();

                    if (query.AspNetRoles.Single().Name == "admin")
                    {
                        return(RedirectToAction("AdminDashboard", "Accounts"));
                    }
                    else if (query.AspNetRoles.Single().Name == "consumer")
                    {
                        return(RedirectToAction("ConsumerDashboard", "Accounts"));
                    }
                }
            }
            return(View());
        }