Пример #1
0
        public ActionResult ExternalLoginCallback(string returnUrl)
        {
            AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
              if (!result.IsSuccessful)
              {
            return RedirectToAction("ExternalLoginFailure");
              }

              using (DataContext db = new DataContext())
              {
            ExternalLoginProfile externalLogin = db.ExternalLoginProfiles.Include("User").FirstOrDefault(p => p.Provider.Equals(result.Provider, StringComparison.OrdinalIgnoreCase) && p.ProviderUserId.Equals(result.ProviderUserId, StringComparison.OrdinalIgnoreCase));
            if (externalLogin != null)
            {
              FormsAuthentication.SetAuthCookie(externalLogin.User.UserName, false);
              return RedirectToLocal(returnUrl);
            }
              }

              using (DataContext db = new DataContext())
              {
            if (User.Identity.IsAuthenticated)
            {
              // If the current user is logged in, add the new account
              ExternalLoginProfile externalLogin = db.ExternalLoginProfiles.FirstOrDefault(p => p.Provider.Equals(result.Provider, StringComparison.OrdinalIgnoreCase) && p.ProviderUserId.Equals(result.ProviderUserId, StringComparison.OrdinalIgnoreCase));
              if (externalLogin == null)
              {
            UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase));
            externalLogin = new ExternalLoginProfile() { User = user, Provider = result.Provider, ProviderUserId = result.ProviderUserId };
            db.ExternalLoginProfiles.Add(externalLogin);
            db.SaveChanges();
              }
              return RedirectToLocal(returnUrl);
            }
            else
            {
              UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.Equals(result.UserName, StringComparison.OrdinalIgnoreCase));
              // Check if user already exists
              if (user == null)
              {
            user = new UserProfile { UserName = result.UserName };
            db.UserProfiles.Add(user);
            ExternalLoginProfile externalLogin = new ExternalLoginProfile() { User = user, Provider = result.Provider, ProviderUserId = result.ProviderUserId };
            db.ExternalLoginProfiles.Add(externalLogin);
            db.SaveChanges();

            FormsAuthentication.SetAuthCookie(externalLogin.User.UserName, false);
            return RedirectToLocal(returnUrl);
              }
              else
              {
            // User is new but that username is taken, ask for their desired user name
            string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId);
            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(result.Provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View("ExternalLoginConfirmation", new RegisterExternalLoginModel { UserName = result.UserName, ExternalLoginData = loginData });
              }
            }
              }
        }
Пример #2
0
 public ActionResult Index()
 {
     HomeModel model = new HomeModel();
     if (User.Identity.IsAuthenticated)
     {
         using (DataContext db = new DataContext())
         {
             var userTasks = db.Tasks.Include("User").Where(t => t.User.UserName.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase)).OrderByDescending(t => t.Id).ToList();
             model.Tasks = userTasks.Where(t => !t.Completed.HasValue).ToList();
             model.ToDoCount = model.Tasks.Count;
             model.CompletedCount = userTasks.Where(t => t.Completed.HasValue).Count();
         }
     }
     return View(model);
 }
Пример #3
0
 public ActionResult Index(HomeModel model)
 {
     if (ModelState.IsValid)
     {
         using (DataContext db = new DataContext())
         {
             UserProfile user = db.UserProfiles.FirstOrDefault(e => e.UserName.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase));
             db.Tasks.Add(new TaskEntry() { Description = model.TaskDescription, Estimation = model.TaskEstimation, User = user });
             db.SaveChanges();
             var userTasks = db.Tasks.Include("User").Where(t => t.User.UserName.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase)).OrderByDescending(t => t.Id).ToList();
             model.Tasks = userTasks.Where(t => !t.Completed.HasValue).ToList();
             model.ToDoCount = model.Tasks.Count;
             model.CompletedCount = userTasks.Where(t => t.Completed.HasValue).Count();
         }
     }
     return View(model);
 }
Пример #4
0
        public ActionResult Disassociate(string provider, string providerUserId)
        {
            string ownerAccount = OAuthWebSecurity.GetUserName(provider, providerUserId);
              ManageMessageId? message = null;

              if (ownerAccount == User.Identity.Name)
              {
            using (DataContext db = new DataContext())
            {
              ExternalLoginProfile externalProfile = db.ExternalLoginProfiles.Include("User").FirstOrDefault(e => e.User.UserName.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase));
              if (externalProfile != null)
              {
            db.ExternalLoginProfiles.Remove(externalProfile);
            db.SaveChanges();
            message = ManageMessageId.RemoveLoginSuccess;
              }
            }
              }

              return RedirectToAction("Manage", new { Message = message });
        }
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<DataContext>(null);

                try
                {
                  using (var context = new DataContext())
                  {
                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);
                }
            }
Пример #6
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
              {
            try
            {
              using (DataContext db = new DataContext())
              {
            UserProfile user = new UserProfile() { UserName = model.UserName, Password = model.Password };
            db.UserProfiles.Add(user);
            db.SaveChanges();
              }
              FormsAuthentication.SetAuthCookie(model.UserName, false);
              return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
              ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
              }

              // If we got this far, something failed, redisplay form
              return View(model);
        }
Пример #7
0
        public ActionResult Manage(LocalPasswordModel model)
        {
            bool hasLocalAccount = false;
              using (DataContext db = new DataContext())
              {
            UserProfile user = db.UserProfiles.FirstOrDefault(e => e.UserName.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase));
            hasLocalAccount = string.IsNullOrEmpty(user.Password);
            ViewBag.HasLocalPassword = hasLocalAccount;
            ViewBag.ReturnUrl = Url.Action("Manage");
            if (hasLocalAccount)
            {
              if (ModelState.IsValid)
              {
            if (user != null && user.Password.Equals(model.OldPassword, StringComparison.OrdinalIgnoreCase))
            {
              user.Password = model.NewPassword;
              db.SaveChanges();
            }
            else
            {
              ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
            }
              }
              return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
            }
            else
            {
              // User does not have a local password so remove any validation errors caused by a missing
              // OldPassword field
              ModelState state = ModelState["OldPassword"];
              if (state != null)
              {
            state.Errors.Clear();
              }

              if (ModelState.IsValid)
              {
            try
            {
              user.Password = model.NewPassword;
              db.SaveChanges();
              return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
            }
            catch (Exception)
            {
              ModelState.AddModelError("", String.Format("Unable to create local account. An account with the name \"{0}\" may already exist.", User.Identity.Name));
            }
              }
            }
              }

              // If we got this far, something failed, redisplay form
              return View(model);
        }
Пример #8
0
 // GET: /Account/Manage
 public ActionResult Manage(ManageMessageId? message)
 {
     ViewBag.StatusMessage =
       message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
       : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
       : message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed."
       : "";
       using (DataContext db = new DataContext())
       {
     UserProfile user = db.UserProfiles.FirstOrDefault(e => e.UserName.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase));
     ViewBag.HasLocalPassword = string.IsNullOrEmpty(user.Password);
       }
       ViewBag.ReturnUrl = Url.Action("Manage");
       return View();
 }
Пример #9
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)
              {
            // Insert a new user into the database
            using (DataContext db = new DataContext())
            {
              UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.Equals(model.UserName, StringComparison.OrdinalIgnoreCase));
              // Check if user already exists
              if (user == null)
              {
            user = new UserProfile { UserName = model.UserName };
            db.UserProfiles.Add(user);
            ExternalLoginProfile externalLogin = new ExternalLoginProfile() { User = user, Provider = provider, ProviderUserId = providerUserId };
            db.ExternalLoginProfiles.Add(externalLogin);
            db.SaveChanges();

            FormsAuthentication.SetAuthCookie(externalLogin.User.UserName, false);
            return RedirectToLocal(returnUrl);
              }
              else
              {
            ModelState.AddModelError("", "User name already exists. Please enter a different user name.");
              }
            }
              }

              ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
              ViewBag.ReturnUrl = returnUrl;
              return View(model);
        }