Exemplo n.º 1
0
        public ActionResult Register(RegisterModel register)
        {
            if (ModelState.IsValid)
            {
                var dbUser = db.Users.FirstOrDefault(u => u.Name == register.Name);

                if (dbUser == null || !string.Equals(dbUser.Name, register.Name))
                {
                    register.Password = MD5Hasher.ComputeHash(register.Password);

                    db.Users.Add(new User
                    {
                        Name     = register.Name,
                        Password = register.Password,
                        IsActive = false
                    });
                    db.SaveChanges();

                    Session["Notification"] = "Your registration is accepted. Wait for review of its portal administrator. Try to log in later, when it is confirmed.";
                    return(RedirectToAction("Notification", "Accounts"));
                }
                else
                {
                    ModelState.AddModelError("", "Such username exists!");
                    return(View(register));
                }
            }

            return(View(register));
        }
Exemplo n.º 2
0
        public ServiceResult Login(UserViewModel viewModel)
        {
            ServiceResult serviceResult = new ServiceResult();

            viewModel.Password = MD5Hasher.ComputeHash(viewModel.Password);

            try
            {
                var user = ((List <User>)userRepository.
                            GetAll(i => i.Username == viewModel.Username &&
                                   i.Password == viewModel.Password).Data).FirstOrDefault();

                if (user != null)
                {
                    user.LastAccessDate = DateTime.Now;

                    userRepository.Update(user);
                }

                serviceResult.Success      = true;
                serviceResult.ResultObject = MapperHelper.Instance.Map <User, UserViewModel>(user);
                serviceResult.Messages.Add(Error.GetErrorMessage(Error.CorrectTransaction));
            }
            catch (Exception ex)
            {
                serviceResult.LogError(ex);
            }

            return(serviceResult);
        }
Exemplo n.º 3
0
        public ServiceResult Register(UserViewModel viewModel)
        {
            return(Insert(viewModel));

            // remove this if the above code works
            ServiceResult serviceResult = new ServiceResult();

            viewModel.Password = MD5Hasher.ComputeHash(viewModel.Password);

            try
            {
                viewModel.RowId = Guid.NewGuid().ToString();
                var user = MapperHelper.Instance.Map <UserViewModel, User>(viewModel);
                user.Created = DateTime.Now;

                userRepository.Insert(user);

                serviceResult.Success = true;
                serviceResult.Messages.Add(Error.GetErrorMessage(Error.CorrectTransaction));
            }
            catch (Exception ex)
            {
                serviceResult.LogError(ex);
            }

            return(serviceResult);
        }
Exemplo n.º 4
0
        public ActionResult Login(LoginModel logon)
        {
            if (ModelState.IsValid)
            {
                var dbUser = db.Users.FirstOrDefault(u => u.Name == logon.Name);

                if (dbUser == null ||
                    dbUser.Password != MD5Hasher.ComputeHash(logon.Password))
                {
                    ModelState.AddModelError("", "Wrong login or password!");
                    return(View(logon));
                }
                else if (dbUser != null &&
                         dbUser.Password == MD5Hasher.ComputeHash(logon.Password) &&
                         string.Equals(dbUser.Name, logon.Name))
                {
                    if (logon.Name == "admin")
                    {
                        Session["IsAdmin"] = true;
                    }
                    else
                    {
                        Session["IsAdmin"] = null;
                    }
                    Session["Name"] = logon.Name;
                    Session["Id"]   = dbUser.Id;

                    return(RedirectToAction("Index", "Home"));
                }
            }

            return(View(logon));
        }
Exemplo n.º 5
0
        public ActionResult Register(RegisterModel register)
        {
            if (ModelState.IsValid)
            {
                var dbUser = db.Users.FirstOrDefault(u => u.Name == register.Name);

                if (dbUser == null || !string.Equals(dbUser.Name, register.Name))
                {
                    register.Password = MD5Hasher.ComputeHash(register.Password);

                    db.Users.Add(new User
                    {
                        Name     = register.Name,
                        Password = register.Password
                    });
                    db.SaveChanges();

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "Such username exists!");
                    return(View(register));
                }
            }

            return(View(register));
        }
Exemplo n.º 6
0
        protected override void Seed(GalleryContext context)
        {
            context.Users.Add(new User
            {
                Name     = "admin",
                Password = MD5Hasher.ComputeHash("admin")
            });

            base.Seed(context);
        }
Exemplo n.º 7
0
        protected override void Seed(MusicPortalContext context)
        {
            context.Users.Add(new User
            {
                Name     = "admin",
                Password = MD5Hasher.ComputeHash("admin"),
                IsActive = true
            });

            base.Seed(context);
        }
Exemplo n.º 8
0
        public ActionResult Create([Bind(Include = "Id,Title,Author,Album,Year")] Song song, string[] selectedGenres, HttpPostedFileBase fileUpload)
        {
            if (ModelState.IsValid)
            {
                song.Genres = new List <Genre>();
                if (selectedGenres != null)
                {
                    foreach (var item in selectedGenres)
                    {
                        int genreId = Convert.ToInt32(item);
                        var genre   = db.Genres.FirstOrDefault(g => g.Id == genreId);
                        if (genre != null)
                        {
                            song.Genres.Add(genre);
                        }
                    }
                }

                if (fileUpload == null)
                {
                    ModelState.AddModelError("", "Файл не указан!");
                }
                string filename   = MD5Hasher.ComputeHash(Path.GetFileName(fileUpload.FileName) + DateTime.Now) + Path.GetExtension(fileUpload.FileName);
                string tempfolder = Server.MapPath("~/Songs");
                if (filename != null)
                {
                    fileUpload.SaveAs(Path.Combine(tempfolder, filename));
                    song.FilePath = filename;
                }

                var userName  = Session["Name"].ToString();
                var publisher = db.Users.FirstOrDefault(u => u.Name == userName);
                song.Publisher = publisher;
                db.Songs.Add(song);
                db.SaveChanges();

                return(View("Index", db.Songs.ToList()));
            }

            return(View(song));
        }
Exemplo n.º 9
0
        public ActionResult Login(LoginModel logon)
        {
            if (ModelState.IsValid)
            {
                var dbUser = db.Users.FirstOrDefault(u => u.Name == logon.Name);

                if (dbUser == null ||
                    dbUser.Password != MD5Hasher.ComputeHash(logon.Password))
                {
                    ModelState.AddModelError("", "Wrong login or password!");
                    return(View(logon));
                }
                else if (dbUser != null &&
                         dbUser.Password == MD5Hasher.ComputeHash(logon.Password) &&
                         string.Equals(dbUser.Name, logon.Name))
                {
                    if (logon.Name == "admin")
                    {
                        Session["IsAdmin"] = true;
                    }
                    else if (!dbUser.IsActive)
                    {
                        Session["Notification"] = "An application for registration, which you have applied, had not yet reviewed the portal administrators try to log in later.";
                        return(RedirectToAction("Notification", "Accounts"));
                    }
                    else
                    {
                        Session["IsAdmin"] = null;
                    }
                    Session["Name"] = logon.Name;
                    Session["Id"]   = dbUser.Id;

                    return(RedirectToAction("Index", "Home"));
                }
            }

            return(View(logon));
        }
        public Credential Map(CreateOrEditViewModel createOrEdit)
        {
            Credential credential = new Credential();

            if (createOrEdit.Id != null)
            {
                credential = this.handler.Storage.GetRepository <ICredentialRepository>().WithKey((int)createOrEdit.Id);
            }

            else
            {
                credential.UserId = createOrEdit.UserId;
            }

            credential.CredentialTypeId = createOrEdit.CredentialTypeId;
            credential.Identifier       = createOrEdit.Identifier;

            if (!string.IsNullOrEmpty(createOrEdit.Secret))
            {
                credential.Secret = createOrEdit.ApplyMd5HashingToSecret ? MD5Hasher.ComputeHash(createOrEdit.Secret) : createOrEdit.Secret;
            }

            return(credential);
        }
 public Credential One(int credentialTypeId, string identifier, string secret) =>
 this.storage.Credentials.FirstOrDefault(c => c.CredentialTypeId == credentialTypeId &&
                                         string.Equals(c.Identifier, identifier, StringComparison.OrdinalIgnoreCase) &&
                                         c.Secret == MD5Hasher.ComputeHash(secret)
                                         );