Ejemplo n.º 1
0
        /// <summary>
        /// Creates a UserInfo class from the User class.
        /// </summary>
        /// <param name="user">User who's data is used to create the UserInfo.</param>
        /// <returns>UserInfo created from User.</returns>
        public static UserInfo FromUser(User user)
        {
            Contract.Requires<ArgumentNullException>(user != null);

            return new UserInfo
            {
                UserId = user.UserId,
                Username = user.Username,
                Email = user.Email,
                FullName = user.FullName
            };
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Issues the FormsAuthenticationTicket to let ASP.NET know that a user is logged in.
        /// </summary>
        /// <param name="user">User that has logged in.</param>
        private void IssueFormsAuthenticationTicket(User user)
        {
            // We need to make a FormsAuthenticationTicket.
            // To store UserInfo data in it we use the 2nd overload.
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                1,
                user.Username,
                DateTime.Now,
                DateTime.Now.AddDays(14),
                true,
                UserInfo.FromUser(user).ToString());

            // Now we encrypt the ticket so no one can read it...
            string encTicket = FormsAuthentication.Encrypt(ticket);

            // ...make a cookie and add it. ASP.NET will now know that our user is logged in.
            Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
        }
Ejemplo n.º 3
0
        public ActionResult Register(User user, string identifier, string returnUrl)
        {
            //The registration form has been submitted
            try
            {
                if (ModelState.IsValid)
                {
                    OpenId openid = new OpenId
                    {
                        OpenIdUrl = identifier,
                        User = user
                    };

                    users.AddOpenId(openid);
                    users.SaveChanges();

                    // Now let's login out user to out application
                    IssueFormsAuthenticationTicket(user);

                    // We're done, let's get back to where we started from.
                    if (string.IsNullOrEmpty(returnUrl))
                        return RedirectToAction("Index", "Home");
                    else
                        return Redirect(returnUrl);
                }

                var registrationModel = new RegistrationViewModel(identifier)
                {
                    Username = user.Username,
                    Email = user.Email,
                    FullName = user.FullName,
                    ReturnUrl = returnUrl
                };

                return View(registrationModel);
            }
            catch
            {
                var registrationModel = new RegistrationViewModel(identifier)
                {
                    Username = user.Username,
                    Email = user.Email,
                    FullName = user.FullName,
                    ReturnUrl = returnUrl
                };

                return View(registrationModel);
            }
        }
Ejemplo n.º 4
0
        public ActionResult Edit(User user)
        {
            try
            {
                // Let's get the user with this id
                var u = users.GetUser(user.UserId);

                // Let's try and update it.
                if (TryUpdateModel<User>(u))
                {
                    // If the update goes through we need to save the changes to the database...
                    users.SaveChanges();

                    // ... and return the user to the User/Index
                    return RedirectToAction("Index");
                }

                ModelState.AddModelError("general_error", "Something went wrong while updating the data.");
            }
            catch (Exception e)
            {
                ModelState.AddModelError("general_error", e);
            }

            // Whatever goes wrong we redisplay the user edit form with as much data as we can get.
            return View(user);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Create a new User object.
 /// </summary>
 /// <param name="userId">Initial value of the UserId property.</param>
 /// <param name="username">Initial value of the Username property.</param>
 /// <param name="email">Initial value of the Email property.</param>
 /// <param name="fullName">Initial value of the FullName property.</param>
 public static User CreateUser(global::System.Int32 userId, global::System.String username, global::System.String email, global::System.String fullName)
 {
     User user = new User();
     user.UserId = userId;
     user.Username = username;
     user.Email = email;
     user.FullName = fullName;
     return user;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Deprecated Method for adding a new object to the Users EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToUsers(User user)
 {
     base.AddObject("Users", user);
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Removes user from the database.
 /// </summary>
 /// <param name="user">User to be removed.</param>
 /// <remarks>User deletion is cascading which means that all of user's OpenIDs will get deleted from the database when the user is deleted.</remarks>
 public void RemoveUser(User user)
 {
     userDb.Users.DeleteObject(user);
 }