Ejemplo n.º 1
0
        /// <summary>
        /// Logins the specified user name.
        /// </summary>
        /// <param name="username">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="rememberMe">The remember me.</param>
        /// <returns></returns>
        public ResponseModel Login(string username, string password, bool rememberMe, bool remoteAuthentication = false)
        {
            string message;
            var    result = false;

            // Controllo delle credenziali
            if (remoteAuthentication)
            {
                result = _loginServices.ManageLoginUserWithoutPassword(username, out message, true);
            }
            else
            {
                result = _loginServices.LoginUserWithEncryptedPassword(username, password, out message, true);
            }

            if (result)
            {
                var User           = GetLoggedUser();
                var userId         = User.ID.Adapt <string>();
                var serializedUser = JsonConvert.SerializeObject(User, Formatting.Indented,
                                                                 new JsonSerializerSettings {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                    MaxDepth = 1
                }
                                                                 );

                var authTicket = new FormsAuthenticationTicket(
                    1,                            // version
                    userId,                       // user name or user id
                    DateTime.Now,                 // created
                    DateTime.Now.AddMinutes(120), // expires
                    rememberMe,                   // persistent?
                    serializedUser                // can be used to store user data
                    );

                var encryptedTicket = FormsAuthentication.Encrypt(authTicket);

                var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                HttpContext.Current.Response.Cookies.Add(authCookie);
            }

            var response = new ResponseModel
            {
                Result  = result,
                Message = message
            };

            return(response);
        }