当前位置: 首页>>代码示例>>C#>>正文


C# IdentityManager.AddUserToRole方法代码示例

本文整理汇总了C#中Microsoft.AspNet.Identity.EntityFramework.IdentityManager.AddUserToRole方法的典型用法代码示例。如果您正苦于以下问题:C# IdentityManager.AddUserToRole方法的具体用法?C# IdentityManager.AddUserToRole怎么用?C# IdentityManager.AddUserToRole使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.AspNet.Identity.EntityFramework.IdentityManager的用法示例。


在下文中一共展示了IdentityManager.AddUserToRole方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: UserRoles

        public ActionResult UserRoles(SelectUserRolesViewModel model)
        {
            if (ModelState.IsValid)
            {
                var db = new ApplicationDbContext();
                var user = db.Users.First(m => m.UserName == model.UserName);
                var im = new IdentityManager();
                im.ClearUserRoles(user.Id);
                foreach (var role in model.Roles)
                {
                    if (role.Selected)
                    {
                        im.AddUserToRole(user.Id, role.RoleName);
                    }
                }

                return RedirectToAction("Index");
            }
            return View();
        }
开发者ID:shirish7151,项目名称:CIMS,代码行数:20,代码来源:AccountController.cs

示例2: UserEdit

        public async Task<ActionResult> UserEdit(EditUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                var Db = new ApplicationDbContext();
                var user = Db.Users.First(u => u.Id == model.UserId);
                if (user != null)
                {
                    user.UserName = model.UserName;
                    user.FirstName = model.FirstName;
                    user.LastName = model.LastName;
                    user.Email = model.Email;
                    user.Role = model.Role;
                    user.BvLocation = Db.BvLocations.Find(model.BvLocationId);

                    var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));

                    if (rm.RoleExists("Admin") && rm.RoleExists("User"))
                    {
                        var idManager = new IdentityManager();
                        if (user.Role == "Admin")
                        {
                            idManager.AddUserToRole(user.Id, "Admin");
                        }
                        if (user.Role == "User")
                        {
                            idManager.AddUserToRole(user.Id, "User");
                        }
                    }


                    Db.Entry(user).State = System.Data.Entity.EntityState.Modified;
                    await Db.SaveChangesAsync();
                }
                return RedirectToAction("UserIndex");
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
开发者ID:JEMMtastic,项目名称:CapstonePhoenix,代码行数:40,代码来源:CRUDController.cs

示例3: UserRoles

 public virtual ActionResult UserRoles(SelectUserRolesViewModel model)
 {
     if (ModelState.IsValid)
     {
         var idManager = new IdentityManager();
         var Db = new ApplicationDbContext();
         var user = Db.Users.First(u => u.UserName == model.UserName);
         idManager.ClearUserRoles(user.Id);
         foreach (var role in model.Roles)
         {
             if (role.Selected)
             {
                 idManager.AddUserToRole(user.Id, role.RoleName);
             }
         }
         return RedirectToAction("index");
     }
     return View();
 }
开发者ID:KavMuthu,项目名称:home-app,代码行数:19,代码来源:AccountController.cs

示例4: UserRoles

        public ActionResult UserRoles(SelectUserRolesViewModel model)
        {
            if (ModelState.IsValid)
            {
                var idManager = new IdentityManager();
                var Db = new ApplicationDbContext();
                var user = Db.Users.First(u => u.UserName == model.UserName);
                idManager.ClearUserRoles(user.Id);

                var allRoles = Db.Roles;
                if (!String.IsNullOrEmpty(model.WebRole))
                {
                    //Try to get this
                    var newRole = allRoles.Where(r => r.Name == model.WebRole).FirstOrDefault();
                    if (newRole != null)
                    {
                        idManager.AddUserToRole(user.Id, newRole.Name);
                    }
                }
                if (!String.IsNullOrEmpty(model.GestorRole))
                {
                    //Try to get this
                    var newRole = allRoles.Where(r => r.Name == model.GestorRole).FirstOrDefault();
                    if (newRole != null)
                    {
                        idManager.AddUserToRole(user.Id, newRole.Name);
                    }
                }

                return RedirectToAction("index");
            }
            return View(model);
        }
开发者ID:felixh0,项目名称:RequestManager,代码行数:33,代码来源:AccountController.cs

示例5: Register

        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                //var user = new ApplicationUser() { UserName = model.UserName };
                var user = model.GetUser();
                var result = await UserManager.CreateAsync(user, model.Password);
                var idManager = new IdentityManager();
                idManager.AddUserToRole(user.Id, "User");
                if (result.Succeeded)
                {
                    //await SignInAsync(user, isPersistent: false);
                    NguoiDung newclone = new NguoiDung();
                    newclone.userid = user.Id;
                    newclone.username = user.UserName;
                    newclone.firstname = user.FirstName;
                    newclone.lastname = user.LastName;
                    newclone.email = user.Email;
                    db1.DSNguoiDung.Add(newclone);
                    db1.SaveChanges();
                    return RedirectToAction("Index", "Account");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
开发者ID:futurify-tainguyen,项目名称:VsmartSell-DashBoard,代码行数:31,代码来源:AccountController.cs

示例6: Register

        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                IdentityManager im = new IdentityManager();
                var user = new ApplicationUser() { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    user = UserManager.FindByName(model.UserName);
                    if (user.UserName == "admin") 
                    {
                        im.AddUserToRole(user.Id, "Admin");
                    }
                    else 
                    {
                        im.AddUserToRole(user.Id, "Waiting");
                    }
                    
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
开发者ID:mdotz,项目名称:Biblioteka-ASP-MVC,代码行数:30,代码来源:AccountController.cs

示例7: Register

        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Save file to disk and retreive calculated file name or null if handled exception occure
                // if user don't provide photo then he don't want photo
                model.PhotoUrl = Utils.SavePhotoFileToDisk(model.Photo, this, null, model.Photo == null ? true : false);

                var user = model.GetUser();

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var idManager = new IdentityManager();
                    idManager.AddUserToRole(user.Id, "User");
                    return RedirectToAction("Index", "Account");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // Si nous sommes arrivés là, un échec s’est produit. Réafficher le formulaire
            return View(model);
        }
开发者ID:jalvarez54,项目名称:NorthWind54,代码行数:27,代码来源:AccountController.cs

示例8: Edit

        public async Task<ActionResult> Edit([Bind(Include = "Id,UserName,HoVaTen,DonVi_ID,Email,TrangThai,RoleId")] EditUserViewModel ht_User)
        {
            if (S4T_HaTinhBase.GetUserSession() == null) return RedirectToAction("Login", "Account", new { returnUrl = Request.Url.PathAndQuery });
            var per = S4T_HaTinhBase.CheckPermission(Request.RequestContext.RouteData.GetRequiredString("controller"));
            if (per != PermissionType.Write) return Content(ExceptionViewer.GetMessage("UPDATE_NOT_PERMISSION"));

            try
            {
                ModelState.Remove("UserName");
                ModelState.Remove("DonVi_ID");
                ModelState.Remove("RoleId");

                var objUserOld = db.AspNetUsers.FirstOrDefault(o => o.Id == ht_User.Id);
                if (objUserOld == null) return JavaScript("Không tìm thấy thông tin người dùng");
                ht_User.DonVi_ID = objUserOld.DonVi_ID;
                if (ModelState.IsValid)
                {
                    objUserOld.HoVaTen = ht_User.HoVaTen;
                    objUserOld.Email = ht_User.Email;
                    objUserOld.TrangThai = ht_User.TrangThai;
                    db.Entry(objUserOld).State = EntityState.Modified;

                    var objUserRoleOld = db.AspNetUserRoles.FirstOrDefault(o => o.UserId == ht_User.Id);
                    if (objUserRoleOld != null && !objUserRoleOld.RoleId.Equals(ht_User.RoleId))
                    {
                        IdentityManager mana = new IdentityManager();

                        // Xóa role cũ cho User
                        mana.ClearUserRoles(ht_User.Id);

                        // Add role mới cho User
                        var roleName = db.AspNetRoles.FirstOrDefault(o => o.Id == ht_User.RoleId).Name;
                        mana.AddUserToRole(ht_User.Id, roleName);
                    }
                    await db.SaveChangesAsync();
                }
                else
                {
                    if (ht_User.DonVi_ID == DonVi.SoThongTinTruyenThong){
                        GetViewBag(NhomDoiTuong.SoTTTT);
                    }
                    else
                        GetViewBag(NhomDoiTuong.DonVi);
                    return View(ht_User);
                }
            }
            catch (DbEntityValidationException ex)
            {
                var sb = new StringBuilder();

                foreach (var failure in ex.EntityValidationErrors)
                {
                    sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
                    foreach (var error in failure.ValidationErrors)
                    {
                        sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
                        sb.AppendLine();
                    }
                }

                throw new DbEntityValidationException(
                    "Entity Validation Failed - errors follow:\n" +
                    sb.ToString(), ex
                ); // Add the original exception as the innerException
            }
            if (ht_User.DonVi_ID == DonVi.SoThongTinTruyenThong)
                return RedirectToAction("ListChuyenVienSo");
            else
                return RedirectToAction("ListUser");
        }
开发者ID:ACETeam90,项目名称:bacanhtai1,代码行数:70,代码来源:Ht_UserController.cs

示例9: UserRoles

 public ActionResult UserRoles(SelectUserRolesViewModel model)
 {
     if (ModelState.IsValid)
     {
         using (var db = new ApplicationDbContext())
         {
             var idManager = new IdentityManager();
             var user = db.Users.First(u => u.UserName == model.UserName);
             idManager.ClearUserRoles(user.Id);
             foreach (var role in model.Roles)
             {
                 if (role.Selected)
                     idManager.AddUserToRole(user.Id, role.RoleName);
             }
             return RedirectToAction("Index");
         }
     }
     // If we got this far, something failed, redisplay form
     return View(model);
 }
开发者ID:marian1709,项目名称:lionsEventManagement,代码行数:20,代码来源:AccountController.cs

示例10: Register

        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {

                var user = new ApplicationUser() { 
                    UserName = model.UserName,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Email = model.Email,
                    PhoneNumber = model.PhoneNumber,
                    Role = model.Role
                    
                };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    TempData["message"] = string.Format("{0} has been registered.", user.UserName);


                    var Db = new ApplicationDbContext();
                    var user2 = Db.Users.First(u => u.UserName == user.UserName);
                    user2.BvLocation = Db.BvLocations.Find(model.BvLocationId);
                    Db.Entry(user2).State = System.Data.Entity.EntityState.Modified;
                    await Db.SaveChangesAsync();

                    var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));

                    if (rm.RoleExists("Admin") && rm.RoleExists("User"))
                    {
                        var idManager = new IdentityManager();
                        if (user.Role == "Admin")
                        {
                            idManager.AddUserToRole(user.Id, "Admin");
                        }
                        if (user.Role == "User")
                        {
                            idManager.AddUserToRole(user.Id, "User");
                        }
                    }

                    return RedirectToAction("UserIndex", "CRUD");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
开发者ID:JEMMtastic,项目名称:CapstonePhoenix,代码行数:53,代码来源:AccountController.cs


注:本文中的Microsoft.AspNet.Identity.EntityFramework.IdentityManager.AddUserToRole方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。