public ActionResult Login(RegisteredUser model, string returnUrl) { using (UsersContext entities = new UsersContext()) { string username = model.UserName; string password = model.Password; Debug.AutoFlush = true; // Now if our password was enctypted or hashed we would have done the // same operation on the user entered password here, But for now // since the password is in plain text lets just authenticate directly var currentUser = entities.RegisteredUsers.SingleOrDefault(user => user.UserName == username); bool userValid=false; if (currentUser != null) { userValid = currentUser.UserName.Equals(username) && currentUser.Password.Equals(password); } // User found in the database if (userValid) { FormsAuthentication.SetAuthCookie(username, false); if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) { return Redirect(returnUrl); } else { UsersContext db = new UsersContext(); RegisteredUser registeredUser = new UsersContext().RegisteredUsers.SingleOrDefault(user => user.UserName == username); registeredUser.IsActivated = "Yes"; db.Entry(registeredUser).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index", "Home"); } } else { ModelState.AddModelError("", "আপনার নাম অথবা পাসওয়ার্ডটি ভুল হয়েছে."); } } // If we got this far, something failed, redisplay form return View(model); }
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); }