Exemplo n.º 1
0
        // In this method we will create default User roles and Admin user for login
        private void CreateRolesandUsers()
        {
            var context = new CashMeContext();

            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
            var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));


            // In Startup iam creating first Admin Role and creating a default Admin User
            if (!roleManager.RoleExists("Admin"))
            {
                // first we create Admin rool
                var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
                role.Name = "Admin";
                roleManager.Create(role);

                //Here we create a Admin super user who will maintain the website

                var user = new ApplicationUser();
                user.UserName = "******";
                user.Email    = "*****@*****.**";

                string userPWD = "A@Z200711";

                var chkUser = UserManager.Create(user, userPWD);

                //Add default User to Role Admin
                if (chkUser.Succeeded)
                {
                    var result1 = UserManager.AddToRole(user.Id, "Admin");
                }
            }

            // creating Creating Manager role
            //if (!roleManager.RoleExists("Manager"))
            //{
            //    var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
            //    role.Name = "Manager";
            //    roleManager.Create(role);

            //}

            // creating Creating Employee role
            //if (!roleManager.RoleExists("Employee"))
            //{
            //    var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
            //    role.Name = "Employee";
            //    roleManager.Create(role);

            //}
        }
Exemplo n.º 2
0
 public Repository(CashMeContext context)
 {
     this._context = context;
 }
Exemplo n.º 3
0
 public BaseRepository(CashMeContext context)
 {
     this._context = context;
     this.Table    = context.Set <T>();
 }
Exemplo n.º 4
0
        public ActionResult UploadUser(FormCollection formCollection)
        {
            if (Request != null)
            {
                CashMeContext      _context = new CashMeContext();
                HttpPostedFileBase file     = Request.Files["UploadedFile"];
                if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
                {
                    string fileName        = file.FileName;
                    string fileContentType = file.ContentType;
                    byte[] fileBytes       = new byte[file.ContentLength];
                    var    data            = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
                    using (var package = new ExcelPackage(file.InputStream))
                    {
                        var currentSheet = package.Workbook.Worksheets;
                        var workSheet    = currentSheet.First();
                        var noOfCol      = workSheet.Dimension.End.Column;
                        var noOfRow      = workSheet.Dimension.End.Row;
                        for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
                        {
                            string email         = workSheet.Cells[rowIterator, 1].Value.ToString();
                            string password      = workSheet.Cells[rowIterator, 2].Value.ToString();
                            string SecurityStamp = workSheet.Cells[rowIterator, 3].Value.ToString();
                            string IP            = workSheet.Cells[rowIterator, 4].Value.ToString();
                            string WalletCode    = workSheet.Cells[rowIterator, 5].Value.ToString();
                            int    Ref           = int.Parse(workSheet.Cells[rowIterator, 6].Value.ToString());

                            string username = Regex.Split(email, "@")[0];

                            var user = new ApplicationUser();
                            user.Email                = email;
                            user.PasswordHash         = password;
                            user.SecurityStamp        = SecurityStamp;
                            user.PhoneNumberConfirmed = false;
                            user.TwoFactorEnabled     = false;
                            user.LockoutEnabled       = true;
                            user.AccessFailedCount    = 1;
                            user.UserName             = username;

                            var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(_context));
                            var store       = new UserStore <ApplicationUser>(_context);
                            var manager     = new UserManager <ApplicationUser>(store);
                            var existUser   = userManager.Users.FirstOrDefault(aa => aa.Id == user.Id);
                            if (existUser != null)
                            {
                                manager.Update(user);
                                var ctx = store.Context;
                                ctx.SaveChanges();
                                var lstRole = new string[] { DefaultData.RoleUser };
                                var allRole = userManager.GetRoles(existUser.Id).ToArray();
                                if (allRole != null && allRole.Any())
                                {
                                    using (TransactionScope scope = new TransactionScope())
                                    {
                                        userManager.RemoveFromRoles(existUser.Id, allRole);
                                        if (lstRole.Length > 0)
                                        {
                                            var roles    = _roleServices.GetAllRoles().Where(aa => lstRole.Contains(aa.Id));
                                            var rolesAdd = roles.Select(aa => aa.Name).ToArray();
                                            userManager.AddToRoles(existUser.Id, rolesAdd);
                                        }
                                        scope.Complete();
                                    }
                                }
                            }
                            else
                            {
                                manager.Create(user);
                                var ctx = store.Context;

                                userManager.AddToRole(user.Id, DefaultData.RoleUser);
                                ctx.SaveChanges();
                            }
                            var wallet = _walletServices.GetWalletbyCode(WalletCode);
                            var uInfo  = new UserInfo();
                            uInfo.UserId        = user.Id;
                            uInfo.WalletId      = wallet.Id;
                            uInfo.Amount        = (wallet.Claim * 25) + (Ref * 5);
                            uInfo.IP            = IP;
                            uInfo.CreateDate    = DateTime.Now;
                            uInfo.LastLoginDate = DateTime.Now;
                            _userInfoServices.InserUserInfo(uInfo);
                            var claim = new Claims();
                            claim.UserId     = user.Id;
                            claim.CountImage = wallet.Claim;
                            _claimsServices.InserClaims(claim);
                        }
                    }
                }
            }
            return(View("Upload"));
        }