Example #1
0
        public void BDInit()
        {
            if (_roleManager.FindByName("Administrator") != null)
            {
                Assert.Fail("DB is already initialized.");
                return;
            }

            // role admin
            string adminRoleName = "Administrator";

            _roleManager.Create(new Role()
            {
                Name = adminRoleName
            });

            // create admin User
            var admin = new FarmaUser
            {
                Email    = "*****@*****.**",
                UserName = "******",
            };
            var success = _userManager.Create(admin);

            if (success.Succeeded)
            {
                _userManager.AddToRole(admin.Id, adminRoleName);
            }
            _userManager.AddPassword(admin.Id, "Qwerty.123$");

            _context.SaveChanges();
        }
        public VentasController()
        {
            var identity = HttpContext.Current.User;

            user            = db.Users.Single(u => u.UserName.Equals(identity.Identity.Name));
            ventasProcessor = new VentasProcess(db);
        }
Example #3
0
            /// <summary>
            /// Esto no se debería usar en prod
            /// </summary>
            /// <param name="context"></param>
            protected override void Seed(FarmaContext context)
            {
                var roleManager = new RoleManager <Role, int>(new RoleStore <Role, int, UserRole>(context));
                var userManager = new UserManager <FarmaUser, int>(new UserStore <FarmaUser, Role, int, UserLogin, UserRole, UserClaim>(context));

                // role admin
                string adminRoleName = "Administrator";

                roleManager.Create(new Role()
                {
                    Name = adminRoleName
                });

                // create admin User
                var admin = new FarmaUser
                {
                    Email    = "*****@*****.**",
                    UserName = "******",
                };
                var success = userManager.Create(admin);

                if (success.Succeeded)
                {
                    userManager.AddToRole(admin.Id, adminRoleName);
                }
                userManager.AddPassword(admin.Id, "Qwerty.123$");
                context.SaveChanges();
            }
        public static AuthenticationProperties CreateProperties(FarmaUser user)
        {
            IDictionary <string, string> data = new Dictionary <string, string>
            {
                { "userName", user.UserName },
                { "id", user.Id.ToString() }
            };

            return(new AuthenticationProperties(data));
        }
        private static async Task ProcessAuthToken(OAuthGrantResourceOwnerCredentialsContext context, FarmaUserManager userManager, FarmaUser user)
        {
            ClaimsIdentity oAuthIdentity =
                await userManager.GenerateUserIdentityAsync(user, OAuthDefaults.AuthenticationType);

            ClaimsIdentity cookiesIdentity =
                await userManager.GenerateUserIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType);

            AuthenticationProperties properties = CreateProperties(user);
            var ticket = new AuthenticationTicket(oAuthIdentity, properties);

            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var userManager = context.OwinContext.GetUserManager <FarmaUserManager>();

            try
            {
                FarmaUser user = await userManager.FindAsync(context.UserName, context.Password);

                if (user == null || user.LockoutEndDateUtc.HasValue)
                {
                    //check user really exists or not
                    user = await userManager.FindByNameAsync(context.UserName);

                    if (user == null)
                    {
                        // non existing user
                        context.SetError("invalid_grant", "The user name or password is incorrect.");
                    }
                    else if (user.LockoutEndDateUtc.HasValue)
                    {
                        await userManager.AccessFailedAsync(user.Id);

                        if (DateTime.UtcNow.CompareTo(user.LockoutEndDateUtc) > 0)
                        {
                            // unlock account if password is valid and let login
                            if (await userManager.CheckPasswordAsync(user, context.Password))
                            {
                                await userManager.ResetAccessFailedCountAsync(user.Id);

                                user.LockoutEndDateUtc = null;
                                await userManager.UpdateAsync(user);
                                await ProcessAuthToken(context, userManager, user);
                            }
                            else
                            {
                                context.SetError("invalid_grant", "The user name or password is incorrect.");
                            }
                        }
                        else
                        {
                            context.SetError("invalid_grant", "User is locked.");
                        }
                    }
                    else
                    {
                        await userManager.AccessFailedAsync(user.Id);

                        context.SetError("invalid_grant", "The user name or password is incorrect.");
                    }
                }
                else
                {
                    await ProcessAuthToken(context, userManager, user);
                }
            }
            catch (Exception)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }
        }
        public static async Task <ClaimsIdentity> GenerateUserIdentityAsync(this FarmaUserManager userManager, FarmaUser user, string authenticationType)
        {
            try
            {
                // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
                var userIdentity = await userManager.CreateIdentityAsync(user, authenticationType);

                // Add custom user claims here
                return(userIdentity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }