Ejemplo n.º 1
0
            public SimpleMembershipInitializer()
            {
                using (var context = new UsersContext())
                    context.UserProfiles.Find(1);

                if (!WebSecurity.Initialized)
                    WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
            }
Ejemplo n.º 2
0
        public ActionResult Create(Ad ad, List<HttpPostedFileBase> photos)
        {
            UserProfile userProfile;
            IList<Photo> dbPhotos;
            Ad newAd;
            using (var userContext = new UsersContext())
            {
                userProfile = userContext.UserProfiles.FirstOrDefault(s => s.UserName == User.Identity.Name);
                if (userProfile == null)
                {
                    return RedirectToAction("Login","Account");
                }
                newAd = new Ad
                {
                    Title = ad.Title,
                    CreateDate = DateTime.UtcNow,
                    Creator = userProfile,
                    Info = ad.Info,
                    Price = ad.Price
                };
                userProfile.Ads.Add(newAd);
                userContext.UserProfiles.AddOrUpdate(userProfile);
                userContext.SaveChanges();

                newAd = userContext.Ads.First(x => x.Id == newAd.Id);

                newAd.Photos = new List<Photo>();
                foreach (var photo in photos)
                {
                    if (photo == null)
                        continue;
                    var path = AppDomain.CurrentDomain.BaseDirectory;
                    newAd.Photos.Add(new Photo
                    {
                        Ad = newAd,
                        Name = photo.FileName,
                        Path = Path.Combine(path, photo.FileName)
                    });
                    photo.SaveAs(Path.Combine(path, photo.FileName));
                }
                userContext.Ads.AddOrUpdate(newAd);
                userContext.SaveChanges();
            }
            return RedirectToAction("Index");
        }
            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("DefaultConnection", "UserProfile", "UserId", "UserName", 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);
                }
            }
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            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 (UsersContext 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);
                    }
                    else
                    {
                        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);
        }
Ejemplo n.º 5
0
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

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

            if (ModelState.IsValid)
            {
                // 将新用户插入到数据库
                using (UsersContext db = new UsersContext())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // 检查用户是否已存在
                    if (user == null)
                    {
                        // 将名称插入到配置文件表
                        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);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "用户名已存在。请输入其他用户名。");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Ejemplo n.º 6
0
        private void HandleUserRole(int? OldAdminPersonId, int? personId)
        {
            int userId = db.Person.First(a => a.Id == personId).UserId;
            int oldUserId = OldAdminPersonId != null ? db.Person.First(a => a.Id == OldAdminPersonId).UserId : -1;

            // Handle Roles and asp.net membership
            if (!Roles.RoleExists("CompanyAdmin"))
                Roles.CreateRole("CompanyAdmin");

            string userName = "";
            string oldUserName = "";
            using (var ctx = new UsersContext())
            {
                userName = ctx.UserProfiles.First(a => a.UserId == userId).UserName;
                oldUserName = oldUserId != -1 ? ctx.UserProfiles.First(a => a.UserId == oldUserId).UserName : "";
            }

            // Remove old user admin
            if (Roles.GetUsersInRole("CompanyAdmin").Contains(oldUserName))
            {
                Roles.RemoveUserFromRole(oldUserName, "CompanyAdmin");
            }

            // Add new user admin
            if (!Roles.GetUsersInRole("CompanyAdmin").Contains(userName))
            {
                Roles.AddUserToRole(userName, "CompanyAdmin");
            }
        }