Ejemplo n.º 1
0
        public async Task <IActionResult> Login(LoginModel model, [FromServices] IGenPasswordHash genPassHash)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            AdministratorUser user = await db.AdministratorUsers.FirstOrDefaultAsync(u => u.Login == model.Login);

            if (user != null)
            {
                if (genPassHash.IsPasswordMathcingHash(model.Password, user.PasswordHash))
                {
                    await Authenticate(user.Id, user.Login, user.Tier); // аутентификация

                    return(RedirectToAction("Products", "Products"));
                }
                else
                {
                    return(RedirectToRoute("ErrorMessage", new { Message = "Password invalid", RedirectRoute = "AdminLogin" }));
                }
            }
            else
            {
                return(RedirectToRoute("ErrorMessage", new { Message = $"Can't find user: {model.Login}", RedirectRoute = "AdminLogin" }));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Login(LoginModel model, [FromServices] IGenPasswordHash genPassHash)
        {
            if (ModelState.IsValid)
            {
                Author user = await db.Authors.FirstOrDefaultAsync(u => (u.Nickname == model.Login || u.Email == model.Login));

                if (user != null)
                {
                    if (genPassHash.IsPasswordMathcingHash(model.Password, user.PasswordHash))
                    {
                        await Authenticate(user.Nickname, user.CanPost);

                        return(RedirectToAction("GetIndex", "Display"));
                    }
                    else
                    {
                        return(RedirectToAction("ErrorMessage", "Utility", new { Message = "Password invalid", Action = "Login", Controller = "Account" }));
                    }
                }
                else
                {
                    return(RedirectToAction("ErrorMessage", "Utility", new { Message = $"cant find user {model.Login}", Action = "Login", Controller = "Account" }));
                }
            }
            return(View(model));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create([FromServices] IGenPasswordHash genPassHash, RegisterModel regModel)
        {
            var dbUser = await db.AdministratorUsers.SingleOrDefaultAsync(e => e.Login == regModel.Login);

            if (dbUser != null)
            {
                ModelState.AddModelError("loginError", "name taken");
            }
            if (!ModelState.IsValid)
            {
                return(View(regModel));
            }
            string passwordHash = genPassHash.GenerateHash(regModel.Password);

            db.AdministratorUsers.Add(new AdministratorUser
            {
                Login        = regModel.Login,
                PasswordHash = passwordHash,
                Tier         = regModel.Tier
            });
            await db.SaveChangesAsync();

            await logger.AddToLogAsync($"Added new account LOGIN:{regModel.Login}");

            return(RedirectToAction("Accounts"));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Register(RegisterModel model, [FromServices] IGenPasswordHash genPassHash)
        {
            if (ModelState.IsValid)
            {
                Author user = await db.Authors.FirstOrDefaultAsync(u => (u.Nickname == model.Nickname || u.Email == model.Email));

                if (user == null)
                {
                    // добавляем пользователя в бд
                    string passwordHash = genPassHash.GenerateHash(model.Password);

                    db.Authors.Add(new Author {
                        Nickname = model.Nickname, Email = model.Email, PasswordHash = passwordHash
                    });
                    await db.SaveChangesAsync();

                    await Authenticate(model.Nickname, false); // аутентификация

                    return(RedirectToAction("GetIndex", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "Пользователь с данным email или nickname уже существует");
                }
            }
            return(View(model));
        }
Ejemplo n.º 5
0
        public TestsFixture()
        {
            genPassword = new PasswordHash();


            var options = new DbContextOptionsBuilder <BoorgerdbContext>()
                          .UseInMemoryDatabase(databaseName: "BoorgerTestingDb")
                          .Options;

            db = new BoorgerdbContext(options);

            db.Tiers.Add(new Tier {
                Value = 15, Description = "Fifteen"
            });
            db.SaveChanges();
            //appEnvironment.WebRootPath


            hostingEnvFake.Setup(m => m.WebRootPath).Returns(@"F:\CSharp\Boorger\Boorger\wwwroot");

            loggerFake.Setup(m => m.Setup(db));
            loggerFake.Setup(m => m.AddToLogAsync(It.IsAny <string>()));



            sessionMock = new ConcurrentDictionary <string, object>();
            sessionHelperMock.Setup(m => m.SetObjectAsJsonAsync(It.IsAny <string>(), It.IsAny <object>()))
            .Callback((string key, object value) => sessionMock.AddOrUpdate(key, value, (_key, oldValue) => value));
            sessionHelperMock.Setup(m => m.RemoveAsync(It.IsAny <string>())).Callback((string key) => sessionMock.TryRemove(key, out _));
            //setup of GetObjectFromJson<T> is completed through calling SetupSession(List<Type> types) with types to paste in T
        }
Ejemplo n.º 6
0
 public UserManagmentLogicValidation(CursedAuthenticationContext db, IErrorHandlerFactory errorHandlerFactory, IHttpContextAccessor contextAccessor, IGenPasswordHash genPasswordHash)
 {
     this.db = db;
     this.errorHandlerFactory = errorHandlerFactory;
     this.contextAccessor     = contextAccessor;
     this.genPasswordHash     = genPasswordHash;
 }
Ejemplo n.º 7
0
 public UserManagmentController(CursedAuthenticationContext db,
                                [FromServices] IGenPasswordHash genPasswordHash,
                                [FromServices] IErrorHandlerFactory errorHandlerFactory,
                                [FromServices] IHttpContextAccessor contextAccessor,
                                [FromServices] ILogProvider <CursedAuthenticationContext> logProvider)
 {
     logic            = new UserManagmentLogic(db, genPasswordHash);
     logicValidation  = new UserManagmentLogicValidation(db, errorHandlerFactory, contextAccessor, genPasswordHash);
     this.logProvider = logProvider;
 }
Ejemplo n.º 8
0
 public AuthenticationController(
     CursedAuthenticationContext db,
     [FromServices] IErrorHandlerFactory errorHandlerFactory,
     [FromServices] IGenPasswordHash genPassHash,
     [FromServices] ILogProvider <CursedAuthenticationContext> logProvider)
 {
     logic            = new AuthenticationLogic(db);
     logicValidation  = new AuthenticationLogicValidation(db, errorHandlerFactory, genPassHash);
     this.logProvider = logProvider;
 }
Ejemplo n.º 9
0
 public UserManagmentLogic(CursedAuthenticationContext db, IGenPasswordHash genPasswordHash)
 {
     this.db = db;
     this.genPasswordHash = genPasswordHash;
 }
Ejemplo n.º 10
0
 public AuthenticationLogicValidation(CursedAuthenticationContext db, IErrorHandlerFactory errorHandlerFactory, IGenPasswordHash genPassHash)
 {
     this.db = db;
     this.errorHandlerFactory = errorHandlerFactory;
     this.genPassHash         = genPassHash;
 }