コード例 #1
0
        public override void AddUsersToRoles(string[] emails, string[] roleNames)
        {
            foreach (string rolename in roleNames)
            {
                if (string.IsNullOrEmpty(rolename))
                    throw new ProviderException("Role name cannot be empty or null.");
                if (!RoleExists(rolename))
                    throw new ProviderException("Role name not found.");
            }

            foreach (string email in emails)
            {
                if (string.IsNullOrEmpty(email))
                    throw new ProviderException("User name cannot be empty or null.");
                if (email.Contains(","))
                    throw new ArgumentException("User names cannot contain commas.");

                if (roleNames.Any(rolename => IsUserInRole(email, rolename)))
                {
                    throw new ProviderException("User is already in role.");
                }
            }

            using (var _db = new UsersContext())
            {

                var profiles =  _db.UserProfiles.Where(
                                                    x =>
                                                    emails.Contains(x.Email));
               var role =     _db.UserRoles.FirstOrDefault(x => roleNames.Contains(x.RoleName));

                /*var emailsInRoles = (from p in profiles
                                     where p.
                                    from r in roles
                                    select new UserInRoleDto {UserId = p.UserId, RoleId = r.RoleId}).Distinct();
                */
                //if (!emailsInRoles.Any())
                //{
                if (role != null && profiles.Any())
                    foreach (var uInr in profiles)
                        _db.UserInRoles.Add(new UserInRole { UserId = uInr.UserId, RoleId = role.RoleId });

                    _db.SaveChanges();
                //}
            }
        }
コード例 #2
0
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<UsersContext>(null);

                try
                {
                    using (var context = new UsersContext())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("LineConnection", "UserProfile", "UserId", "Email", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
コード例 #3
0
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider;
            string providerUserId;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (var db = new UsersContext())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
コード例 #4
0
        public override string[] GetRolesForUser(string username)
        {
            var role = new string[]{};

            using (var _db = new UsersContext())
            {
                try
                {
                    UserProfile user = (from u in _db.UserProfiles
                                        where u.UserName == username
                                        select u).FirstOrDefault();

                    if (user != null)
                    {
                        // получаем роль
                        var roles = from r in _db.UserInRoles
                                    from w in _db.UserRoles
                                    where r.UserId == user.UserId
                                    where w.RoleId == r.RoleId
                                    select w;

                        if (roles.Any())
                        {
                            role = roles.Select(x=>x.RoleName).ToArray();
                        }
                    }
                }
                catch
                {
                    role = new string[] { };
                }
            }

            return role;
        }
コード例 #5
0
        public override bool RoleExists(string roleName)
        {
            if (string.IsNullOrEmpty(roleName))
                throw new ProviderException("Role name cannot be empty or null.");

            bool exists = false;

            using (var _db = new UsersContext())
            {
                exists = _db.UserRoles.Select(m => m.RoleName == roleName).ToList().Count > 0;
            }

            return exists;
        }
コード例 #6
0
        public override bool IsUserInRole(string email, string roleName)
        {
            bool outputResult = false;
            using (var _db = new UsersContext())
            {
                try
                {
                    UserProfile user = (from u in _db.UserProfiles
                                 where u.Email == email
                                 select u).FirstOrDefault();
                    if (user != null)
                    {
                        var roles = from r in _db.UserInRoles
                                    from w in _db.UserRoles
                                    where r.UserId == user.UserId
                                    where w.RoleId == r.RoleId
                                    select w;

                        if (!roles.Any())
                            return false;

                        outputResult = roles.Any(x => x.RoleName == roleName);

                    }
                }
                catch
                {
                    outputResult = false;
                }
            }
            return outputResult;
        }