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 (RIOContext db = new RIOContext()) { User user = db.User.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower()); // Check if user already exists if (user == null) { // Insert name into the profile table //db.User.Add(new User { UserName = model.UserName }); User newUser = db.User.Add(new User { UserName = model.UserName }); db.SaveChanges(); db.ExternalUsers.Add(new ExternalUserInformation { UserId = newUser.UserId, FullName = model.FullName, Link = model.Link }); 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)); }
public ActionResult Create(ItemCreateViewModel viewModel) { if (ModelState.IsValid) { // Initializes the new item object Item item = new Item(); // Logic for uploading image if (viewModel.Image != null) { // Gets the root upload directory string uploadFolder = Server.MapPath(ConfigurationHelper.UploadFolder); // Creates directory if doesn't exist if (!Directory.Exists(uploadFolder)) { Directory.CreateDirectory(uploadFolder); } // Generates unique file name by using Guid string fileName = string.Concat(Guid.NewGuid(), "_", viewModel.Image.FileName); // Concatenates root upload folder and file name string filePath = string.Concat(uploadFolder, fileName); // Saves image to the path viewModel.Image.SaveAs(filePath); // Sets image path to view model viewModel.ImagePath = string.Concat(ConfigurationHelper.UploadFolder, fileName); // Initializes item images object item.Images = new List <ItemImage>(); // Adds the image to item image collection item.Images.Add(new ItemImage { ImagePath = viewModel.ImagePath }); // Logic for thumbnail image which can be used by search page WebImage webImage = new WebImage(viewModel.Image.InputStream); // Resize image webImage.Resize(Convert.ToInt32(ConfigurationHelper.ThumbnailWidth), Convert.ToInt32(ConfigurationHelper.ThumbnailHeight), preserveAspectRatio: true); // Saves Thumbnail webImage.Save(string.Concat(uploadFolder, "Thumbnail_", fileName)); // Adds Thumbnail image to item images collection with IsThumbnail flag true item.Images.Add(new ItemImage { ImagePath = string.Concat(ConfigurationHelper.UploadFolder, "Thumbnail_", fileName), IsThumbnail = true }); } // Assign properties from view model to actual item model object item.Category = db.Category.FirstOrDefault(p => p.CategoryId == viewModel.SelectedCategoryId); item.Brand = db.Brand.FirstOrDefault(p => p.BrandId == viewModel.SelectedBrandId); item.Address = db.Address.FirstOrDefault(p => p.AddressId == viewModel.SelectedAddressId); item.ItemName = viewModel.ItemName; item.ItemDescription = viewModel.ItemDescription; item.Phone = viewModel.Phone.Value; // Saves Costing item.ItemCosting = new List <ItemCosting>(); item.ItemCosting.Add(new ItemCosting { CostingId = viewModel.SelectedCostingId.Value }); // Saves ID Proof item.RequiredDocuments = new List <ItemRequiredDocument>(); if (viewModel.SelectedIdentityProofId != null) { foreach (int id in viewModel.SelectedIdentityProofId) { item.RequiredDocuments.Add(new ItemRequiredDocument { IdentityProofId = id }); } } // Adds new item to db db.Item.Add(item); // Save changes db.SaveChanges(); return(RedirectToAction("Index", "Home", null)); } viewModel.Category = new SelectList(db.Category, "CategoryId", "CategoryName", "SelectedCategoryId"); viewModel.Brand = new SelectList(db.Brand, "BrandId", "BrandName", "SelectedBrandId"); viewModel.Address = new SelectList(db.Address, "AddressId", "AddressLine1", "SelectedAddressId"); viewModel.Costing = new SelectList(db.Costing, "CostingId", "Name", "SelectedCostingId"); viewModel.IdentityProof = db.IdentityProof; return(View(viewModel)); }