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); }
/// <summary> /// Retrieves a list of post view models (both recipes and blogs) and orders them by date (newest first). /// </summary> /// <param name="recipes">The recipes to create view models for</param> /// <param name="blogPosts">The blog posts to create view models for</param> /// <param name="page">The current page number</param> /// <param name="ViewBag"></param> /// <returns>Date ordered list of recipes/blogs</returns> public static List<ViewPostModel> GetCombinedPosts(List<Recipe> recipes, List<BlogPost> blogPosts, int page, dynamic ViewBag) { CookbookDBModelsDataContext db = new CookbookDBModelsDataContext(); UsersContext userDb = new UsersContext(); List<ViewPostModel> postList = new List<ViewPostModel>(); foreach (var recipe in recipes) { ViewRecipeModel recipeView = new ViewRecipeModel(); recipeView.DateModified = recipe.DateModified; recipeView.FavoriteCount = recipe.FavoriteCount; recipeView.Instructions = recipe.Instructions; recipeView.LikeCount = recipe.LikeCount; var ingredients = (from allIngredients in db.Ingredients where allIngredients.RecipeId == recipe.RecipeID select allIngredients.Name).ToList(); recipeView.Ingredients = ingredients; var tags = (from allTags in db.Recipe_Tags where allTags.RecipeID == recipe.RecipeID select allTags.Tag).ToList(); recipeView.Tags = tags; ViewPostModel post = new ViewPostModel(); post.DateCreated = recipe.DateCreated; post.RecipePost = recipeView; post.Username = (from userprofiles in userDb.UserProfiles where userprofiles.UserId == recipe.UserID select userprofiles.UserName).FirstOrDefault(); post.ImageURL = recipe.ImageUrl; post.Title = recipe.Title; post.PostId = recipe.RecipeID; post.UserID = recipe.UserID; if (db.Recipe_Likers.Contains(new Recipe_Liker { UserId = WebSecurity.CurrentUserId, RecipeId = post.PostId })) { post.Liked = true; } else { post.Liked = false; } if (db.Recipe_Favoriters.Contains(new Recipe_Favoriter { UserId = WebSecurity.CurrentUserId, RecipeId = post.PostId })) { post.RecipePost.Favorited = true; } else { post.RecipePost.Favorited = false; } postList.Add(post); } foreach (var blog in blogPosts) { ViewBlogModel blogView = new ViewBlogModel(); blogView.DateModified = blog.DateModified; blogView.LikeCount = blog.LikeCount; blogView.Post = blog.Post; var tags = (from allTags in db.BlogPost_Tags where allTags.BlogPostId == blog.BlogPostId select allTags.Tag).ToList(); blogView.Tags = tags; ViewPostModel post = new ViewPostModel(); post.DateCreated = blog.DateCreated; post.BlogPost = blogView; post.Username = (from userprofiles in userDb.UserProfiles where userprofiles.UserId == blog.UserId select userprofiles.UserName).FirstOrDefault(); post.ImageURL = blog.ImageUrl; post.Title = blog.Title; post.PostId = blog.BlogPostId; post.UserID = blog.UserId; if (db.BlogPost_Likers.Contains(new BlogPost_Liker { UserId = WebSecurity.CurrentUserId, BlogPostId = post.PostId })) { post.Liked = true; } else { post.Liked = false; } postList.Add(post); } int pageLength = 15; int startPage = (page - 1) * pageLength; ViewBag.Page = (int)page; ViewBag.LastPage = (int)Math.Ceiling((double)postList.Count() / pageLength); if (startPage < postList.Count) { if (startPage + pageLength - 1 >= postList.Count) { pageLength = postList.Count - startPage; } return postList.OrderByDescending(p => p.DateCreated).ToList().GetRange(startPage, pageLength); } else { return new List<ViewPostModel>();//there are no results in this page, return an empty list } }