コード例 #1
0
        private void ChangeAdminSettings()
        {
            string username = Request.Params["ctl00$MainContent$AdminUsernameTB"];
            string email = Request.Params["ctl00$MainContent$AdminEmailTB"];
            string password = Request.Params["ctl00$MainContent$PasswordTB"];
            string confirm = Request.Params["ctl00$MainContent$ConfirmTB"];

            ConnectorDataContext db = new ConnectorDataContext();

            Stopwatch w = Stopwatch.StartNew();
            User admin = db.Users.Where(u => u.isAdmin).Single();
            w.Stop();
            ILog log = LogManager.GetLogger("QueryLogger");
            log.Info(" Elapsed time: " + w.Elapsed + ", select the admin to change his settings");
            bool changePassword = true;

            if (ChangePasswordCB.Checked)
                if (password.Equals(confirm))
                    admin.password = db.Encrypt(password);
                else
                {
                    ErrorPA.Attributes.Add("class", "error");
                    ErrorPA.InnerText = "Passwords do not match.";
                    changePassword = false;
                }

            if (changePassword)
            {
                Stopwatch w2 = Stopwatch.StartNew();
                bool usr = db.Users.Any(u => (u.username == username || u.email == email) && !u.isAdmin);
                w2.Stop();
                ILog log2 = LogManager.GetLogger("QueryLogger");
                log2.Info(" Elapsed time: " + w2.Elapsed + ", check if there is an user with admin's username or email");
                if (!usr)
                {
                    admin.username = username;
                    admin.email = email;

                    Stopwatch w3 = Stopwatch.StartNew();
                    db.SubmitChanges();
                    w3.Stop();
                    ILog log3 = LogManager.GetLogger("QueryLogger");
                    log3.Info(" Elapsed time: " + w3.Elapsed + ", change admin settings");
                    ErrorPA.Attributes.Add("class", "confirm");
                    ErrorPA.InnerText = "Data stored";
                }
                else
                {
                    ErrorPA.Attributes.Add("class", "error");
                    ErrorPA.InnerText = "Username or email already exist.";
                }
            }
        }
コード例 #2
0
        private void SaveUsers()
        {
            ConnectorDataContext db = new ConnectorDataContext();

            XmlDocument requestXml = new XmlDocument();
            requestXml.Load(new XmlTextReader(new StreamReader(Request.InputStream)));

            List<string> mailError = new List<string>();

            foreach (XmlNode item in requestXml.SelectNodes("//users/user"))
            {
                try
                {
                    String passwd = Membership.GeneratePassword(10, 2);
                    User user = new User()
                    {
                        username = item.InnerText,
                        email = item.InnerText,
                        password = db.Encrypt(passwd)
                    };
                    Stopwatch w = Stopwatch.StartNew();
                    db.Users.InsertOnSubmit(user);
                    w.Stop();
                    ILog log = LogManager.GetLogger("QueryLogger");
                    log.Info(" Elapsed time: " + w.Elapsed + ", insert the user in a pending state");

                    if (WebUtility.SendEmail(item.InnerText, "SocialCDE invitation", GetBody(item.InnerText, passwd), true))
                    {
                        Stopwatch w1 = Stopwatch.StartNew();
                        db.SubmitChanges();
                        w1.Stop();
                        ILog log1 = LogManager.GetLogger("QueryLogger");
                        log1.Info(" Elapsed time: " + w1.Elapsed + ", send mail for registration");
                    }
                    else
                        mailError.Add(item.InnerText);
                }
                catch
                {
                    mailError.Add(item.InnerText);
                }
            }

            XElement root = new XElement("Root");
            foreach (string item in mailError)
                root.Add(new XElement("NotSent", item));

            Response.Clear();
            Response.ContentType = "text/xml";
            Response.Write(new XDocument(root));
            Response.End();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ConnectorDataContext db = new ConnectorDataContext();

            String token = Request.QueryString["token"];
            Setting recoveringToken = null;
            Setting recoveringTime = null;

            try
            {
                Stopwatch w = Stopwatch.StartNew();
                recoveringTime = db.Settings.Where(s => s.key == "RecoveringTime").Single();
                w.Stop();
                ILog log = LogManager.GetLogger("QueryLogger");
                log.Info(" Elapsed time: " + w.Elapsed + ", select the 'recovering time' key from settings");
                Stopwatch w1 = Stopwatch.StartNew();
                recoveringToken = db.Settings.Where(s => s.key == "RecoveringToken").Single();
                w1.Stop();
                ILog log1 = LogManager.GetLogger("QueryLogger");
                log1.Info(" Elapsed time: " + w1.Elapsed + ", select the 'recovering token' key from settings");
            }
            catch { }

            if (Request.RequestType == "GET")
            {
                if (String.IsNullOrEmpty(token))
                {
                    if (recoveringTime == null || DateTime.Parse(recoveringTime.value) < DateTime.UtcNow - new TimeSpan(0, 5, 0))
                    {
                        String newToken = GenerateToken();

                        Stopwatch w2 = Stopwatch.StartNew();
                        String to = db.Users.Where(u => u.isAdmin).Single().email;
                        w2.Stop();
                        ILog log2 = LogManager.GetLogger("QueryLogger");
                        log2.Info(" Elapsed time: " + w2.Elapsed + ", select the admin's email");

                        if (WebUtility.SendEmail(to, "Password recovering", GetBody(newToken), true))
                        {
                            if (recoveringToken != null)
                            {
                                recoveringToken.value = newToken;
                                recoveringTime.value = DateTime.UtcNow.ToString();
                            }
                            else
                            {
                                Stopwatch w3 = Stopwatch.StartNew();
                                db.Settings.InsertAllOnSubmit(
                                    new List<Setting>(){
                                new Setting () {
                                    key = "RecoveringToken",
                                    value = newToken
                                },
                                new Setting () {
                                    key = "RecoveringTime",
                                    value = DateTime.UtcNow.ToString()
                                }});
                                w3.Stop();
                                ILog log3 = LogManager.GetLogger("QueryLogger");
                                log3.Info(" Elapsed time: " + w3.Elapsed + ", insert new setting in a pending state(password recovering)");
                            }
                            Stopwatch w4 = Stopwatch.StartNew();
                            db.SubmitChanges();
                            w4.Stop();
                            ILog log4 = LogManager.GetLogger("QueryLogger");
                            log4.Info(" Elapsed time: " + w4.Elapsed + ", insert new settings");
                            Response.Redirect("Login.aspx?type=confirm&message=Email sent, check your email inbox.");
                        }
                        else
                            Response.Redirect("Login.aspx?type=error&message=Is not possible recover the password, the smtp server is not set.");
                    }
                    else
                        Response.Redirect("Login.aspx?type=error&message=You have sent a request less than 5 minutes ago. Please, try again later.");
                }
                else
                {
                    if (recoveringToken == null || recoveringToken.value != token)
                        Response.Redirect("Login.aspx?type=error&message=Wrong token.");
                }
            }
            else if (Request.RequestType == "POST")
            {
                Stopwatch w5 = Stopwatch.StartNew();
                db.Users.Where(u => u.isAdmin).Single().password = db.Encrypt(Request.Params["ctl00$MainContent$PasswordTB"]);
                w5.Stop();
                ILog log5 = LogManager.GetLogger("QueryLogger");
                log5.Info(" Elapsed time: " + w5.Elapsed + ", select admin's password");
                Stopwatch w6 = Stopwatch.StartNew();
                db.Settings.DeleteAllOnSubmit(db.Settings.Where(s => s.key == "RecoveringToken" || s.key == "RecoveringTime"));
                db.SubmitChanges();
                w6.Stop();
                ILog log6 = LogManager.GetLogger("QueryLogger");
                log6.Info(" Elapsed time: " + w6.Elapsed + ", password changed");
                Response.Redirect("Login.aspx?type=confirm&message=Password changed successfully.");
            }
        }
        public int SubscribeUser(String email, String password, String username)
        {
            Contract.Requires(!String.IsNullOrEmpty(email));
            Contract.Requires(!String.IsNullOrEmpty(password));
            Contract.Requires(!String.IsNullOrEmpty(username));

            ConnectorDataContext db = new ConnectorDataContext();
            User user;
            try
            {
                Stopwatch w = Stopwatch.StartNew();
                user = db.Users.Where(u => u.email == email).Single();
                w.Stop();
                ILog log = LogManager.GetLogger("QueryLogger");
                log.Info(" Elapsed time: " + w.Elapsed + ", user email: " + email + ", select the user to subscribe him");
            }
            catch (InvalidOperationException)
            {
                return 1;
            }

            if (user.password != db.Encrypt(password))
                return 2;

            if (!IsAvailable(username))
                return 3;

            user.username = username;
            user.active = true;

            Stopwatch w1 = Stopwatch.StartNew();
            int sInstance = db.ServiceInstances.Where(si => si.Service.name == "SocialTFS").Single().id;
            w1.Stop();
            ILog log1 = LogManager.GetLogger("QueryLogger");
            log1.Info(" Elapsed time: " + w1.Elapsed + ", select the service instance with name 'SocialTFS'");

            Registration registration = new Registration()
            {
                User = user,
                serviceInstance = sInstance,
                nameOnService = username,
                idOnService = username
            };
            Stopwatch w2 = Stopwatch.StartNew();
            db.Registrations.InsertOnSubmit(registration);
            db.SubmitChanges();
            w2.Stop();
            ILog log2 = LogManager.GetLogger("QueryLogger");
            log2.Info(" Elapsed time: " + w2.Elapsed + ", service instance's id: " + sInstance + ", name and id on service: " + username + ", insert a new registration");

            Stopwatch w3 = Stopwatch.StartNew();
            db.ChosenFeatures.InsertOnSubmit(new ChosenFeature()
            {
                Registration = registration,
                feature = FeaturesType.Post.ToString(),
                lastDownload = new DateTime(1900, 1, 1)
            });
            db.SubmitChanges();
            w3.Stop();
            ILog log3 = LogManager.GetLogger("QueryLogger");
            log3.Info(" Elapsed time: " + w3.Elapsed + ", feature: " + FeaturesType.Post.ToString() + ", last download: " + new DateTime(1900, 1, 1) + ", insert a new Chosen feature");

            return 0;
        }
        public bool ChangePassword(String username, String oldPassword, String newPassword)
        {
            Contract.Requires(!String.IsNullOrEmpty(username));
            Contract.Requires(!String.IsNullOrEmpty(oldPassword));
            Contract.Requires(!String.IsNullOrEmpty(newPassword));

            ConnectorDataContext db = new ConnectorDataContext();

            User user = CheckCredentials(db, username, oldPassword);
            if (user == null)
                return false;

            Stopwatch w = Stopwatch.StartNew();
            user.password = db.Encrypt(newPassword);
            db.SubmitChanges();
            w.Stop();
            ILog log = LogManager.GetLogger("QueryLogger");
            log.Info(" Elapsed time: " + w.Elapsed + ", change password");
            return true;
        }
        private User CheckCredentials(ConnectorDataContext db, String username, String password)
        {
            Contract.Requires(!String.IsNullOrEmpty(username));
            Contract.Requires(!String.IsNullOrEmpty(password));

            try
            {
                Stopwatch w = Stopwatch.StartNew();
                User user = db.Users.Where(u => u.username == username && u.password == db.Encrypt(password) && u.active).Single();
                w.Stop();
                ILog log = LogManager.GetLogger("QueryLogger");
                log.Info(" Elapsed time: " + w.Elapsed + ", username: "******", password: ****** , check credentials");
                return user;
            }
            catch (InvalidOperationException)
            {
                return null;
            }
        }