Exemple #1
0
        public IQueryable GetClients()
        {
            var        _db   = new ReportsForOZHIdev.Models.ClientsContext();
            IQueryable query = _db.Clients;

            return(query);
        }
        internal void AddUser(string role, string username, string password, string firstname, string lastname, string company)
        {
            // Access application context and create result variables
            Models.ApplicationDbContext context = new ApplicationDbContext();
            IdentityResult IdRoleResult;
            IdentityResult IdUserResult;

            // Create a RoleStore object by using the ApplicationDbContext object
            // The RoleStore is only allowed to contain IdentityRole objects
            var roleStore = new RoleStore <IdentityRole>(context);

            // Create a RoleManager object that is only allowed to contain IdentityRole objects
            // When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object
            var roleMgr = new RoleManager <IdentityRole>(roleStore);

            // Then, you create the "canEdit" role if it doesn't already exist
            if (!roleMgr.RoleExists(role))
            {
                IdRoleResult = roleMgr.Create(new IdentityRole {
                    Name = role
                });
            }

            // Create UserManager object based on the UserStore object and the ApplicationDbContext
            // object.  Note that you can create new objects and use them as parameters in
            // a single line of code, rather than using multiple lines of code, as you did
            // for the RoleManager object
            var userMgr = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));
            var appUser = new ApplicationUser
            {
                UserName = username,
                Email    = username
            };

            IdUserResult = userMgr.Create(appUser, password);

            // If the new "canEdit" user was successfully created,
            // add the "canEdit" user to the "canEdit" role
            if (!userMgr.IsInRole(userMgr.FindByEmail(username).Id, role))
            {
                IdUserResult = userMgr.AddToRole(userMgr.FindByEmail(username).Id, role);
            }

            using (var _db = new ReportsForOZHIdev.Models.ClientsContext())
            {
                var myUser = new User();
                var user   = userMgr.FindByEmail(username);
                myUser.UserId       = user.Id;
                myUser.Username     = user.UserName;
                myUser.Email        = user.Email;
                myUser.PasswordHash = user.PasswordHash;
                myUser.FirstName    = firstname;
                myUser.LastName     = lastname;
                myUser.Company      = company;

                _db.Users.Add(myUser);
                _db.SaveChanges();
            }
        }
Exemple #3
0
        public IQueryable <User> GetUser([QueryString("IDUser")] int?IdUser)
        {
            var _db = new ReportsForOZHIdev.Models.ClientsContext();
            IQueryable <User> query = _db.Users;

            if (IdUser.HasValue && IdUser >= 0)
            {
                query.Where(u => u.IDUser == IdUser);
            }
            else
            {
                query = null;
            }
            return(query);
        }