示例#1
0
        public static UserOrganization ToUserOrganizationEntity(bool IsActive, UserBO User, OrganizationBO Organization)
        {
            UserOrganization UserOrganization = new UserOrganization();

            UserOrganization.Active = IsActive;

            UserOrganization.RoleId = User.Role;

            User         UserInfo         = new EF.User();
            Organization OrganizationInfo = new EF.Organization();

            UserInfo.EmailAddress  = User.EmailAddress;
            UserInfo.UserName      = User.EmailAddress;
            UserInfo.LastName      = User.LastName;
            UserInfo.FirstName     = User.FirstName;
            UserInfo.PhoneNumber   = User.PhoneNumber;
            UserInfo.ResetPassword = User.ResetPassword; //false;
            UserInfo.PasswordHash  = User.PasswordHash;  //"PassWord1";
            UserInfo.UGuid         = User.UGuid;
            UserOrganization.User  = UserInfo;


            OrganizationInfo.Organization1   = Organization.Organization;
            OrganizationInfo.IsEnabled       = Organization.IsEnabled;
            OrganizationInfo.OrganizationKey = Organization.OrganizationKey;

            UserOrganization.Organization = OrganizationInfo;

            return(UserOrganization);
        }
示例#2
0
        public async Task <IActionResult> New([Bind(Prefix = "Item1")] EF.User args, [Bind(Prefix = "Item2")] bool isactive)
        {
            ViewBag.Roles = new BLL.Role(unitOfWork).DropDown();

            try
            {
                if (!ModelState.IsValid)
                {
                    return(View());
                }

                // Add user
                args.DateCreated = DateTime.Now;

                if (!isactive)
                {
                    args.DateInactive = DateTime.Now;
                }

                await new BLL.User(unitOfWork).Add(args);

                return(Redirect("~/Admin/User"));
            }
            catch (DbUpdateException ex)
            {
                ModelState.AddModelError(string.Empty, "Entry is causing conflict or already exists");
                return(View());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
示例#3
0
        public async Task <bool> Update(EF.User entity)
        {
            var code = entity.Code.ToSafetyString().ToUpper();
            var item = await _dbContext.Users.FirstOrDefaultAsync(x => x.ID == entity.ID);

            item.Username   = entity.Username;
            item.Code       = code;
            item.FullName   = entity.FullName;
            item.Email      = entity.Email;
            item.Skype      = entity.Skype;
            item.Permission = entity.Permission;
            item.Alias      = entity.Alias;
            try
            {
                await _dbContext.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                //logging
                return(false);
            }
        }
        public async Task <IActionResult> SignUp(EF.User args)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View());
                }

                // Add user
                args.DateCreated = DateTime.Now;
                await new BLL.User(unitOfWork).Add(args);

                var smtpSettings     = (ViewBag.Settings as List <EF.Setting>).Where(x => x.Group == "Smtp");
                var smtpDisplayName  = smtpSettings.First(x => x.Key == "Smtp display name").Value;
                var smtpDisplayEmail = smtpSettings.First(x => x.Key == "Smtp display email").Value;
                var smtpServer       = smtpSettings.First(x => x.Key == "Smtp server").Value;
                var smtpPort         = smtpSettings.First(x => x.Key == "Smtp port").Value;
                var smtpUseSsl       = smtpSettings.First(x => x.Key == "Smtp use ssl").Value;
                var smtpUser         = smtpSettings.First(x => x.Key == "Smtp user").Value;
                var smtpPwd          = smtpSettings.First(x => x.Key == "Smtp password").Value;

                // Send email re: password
                var msg = new MimeMessage();

                msg.From.Add(new MailboxAddress(smtpDisplayName, smtpDisplayEmail));
                msg.To.Add(new MailboxAddress("", args.Email));
                msg.Subject = "Your password";
                var bodyBuilder = new BodyBuilder();
                bodyBuilder.HtmlBody = "Click the link below to change your password:<br/>http://" + Request.Host.Value + "/authorization/confirmation/?userid=" + args.UserId + "&code=" + args.ConfirmationCode;
                msg.Body             = bodyBuilder.ToMessageBody();

                using (var client = new SmtpClient())
                {
                    client.Connect(smtpServer, Convert.ToInt32(smtpPort), Convert.ToBoolean(smtpUseSsl));

                    // Note: only needed if the SMTP server requires authentication
                    client.Authenticate(smtpUser, smtpPwd);

                    client.Send(msg);
                    client.Disconnect(true);
                }

                TempData["notice"] = "Thank you. Please check your email for registration confirmation.";
                return(Redirect("~/SignIn"));
            }
            catch (DbUpdateException ex)
            {
                ModelState.AddModelError(string.Empty, "Entry is causing conflict or already exists");
                return(View());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
示例#5
0
文件: DbUser.cs 项目: pero101/ToDo
        public static Status RegisterUser(string username, string password, string email)
        {
            EF.User newUser = new EF.User();
            newUser.UserName = username;
            newUser.Password = password;
            newUser.EMail = email;

            DbConnector.GetConnection().Users.Add(newUser);
            DbConnector.GetConnection().SaveChanges();

            return Status.Created;
        }
示例#6
0
文件: DbUser.cs 项目: pero101/ToDo
        public static Status CheckCanUserRegisterd(string username, string password, string email)
        {
            EF.User newUser = new EF.User();
            newUser.UserName = username;
            newUser.Password = password;
            newUser.EMail = email;

            var user = from x in DbConnector.GetConnection().Users
                       where x.UserName == username || x.EMail == email
                       select x;

            if (user.Any())
                return Status.Ok;
            else
                return Status.NotFoound;
        }
        public async Task <IActionResult> SignIn(EF.User args)
        {
            var buser = new BLL.User(unitOfWork);

            var user = await buser.Get(new EF.User {
                Email = args.Email
            });

            if (user != null)
            {
                // Check if active
                if (user.DateInactive != null)
                {
                    ModelState.AddModelError(string.Empty, "Account is inactive.");
                    return(View(args));
                }

                // Check password
                var salt      = user.PasswordSalt;
                var saltBytes = Convert.FromBase64String(salt);

                if (Core.Crypto.Hash(args.PasswordHash, saltBytes) == user.PasswordHash)
                {
                    // Successful log in
                    user.LastSessionId = Guid.NewGuid().ToString();
                    user.LastLoginDate = DateTime.Now;
                    await buser.Edit(user);

                    await AddClaimsAndSignIn(user);

                    if (user.RoleId == (int)BLL.User.Roles.User)
                    {
                        return(Redirect("~/Main"));
                    }
                    else
                    {
                        return(Redirect("~/Admin"));
                    }
                }
            }

            ModelState.AddModelError(string.Empty, "Access denied.");
            return(View(args));
        }
示例#8
0
        public bool Update(EF.User entity)
        {
            var code = entity.Code.ToUpper().ToSafetyString();
            var item = _dbContext.Users.FirstOrDefault(x => x.ID == entity.ID);

            item.Username = entity.Username;
            item.Code     = code;
            item.FullName = entity.FullName;
            item.Role     = entity.Role;
            item.LevelID  = entity.LevelID;
            try
            {
                _dbContext.SaveChanges();
                return(true);
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                //logging
                return(false);
            }
        }
        /// <summary>
        /// This will signin the user
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        private async Task AddClaimsAndSignIn(EF.User args)
        {
            var ci = new ClaimsIdentity(
                new[]
            {
                // User info
                new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),
                new Claim(ClaimTypes.Name, args.FirstName + " " + args.LastName),
                new Claim(ClaimTypes.Email, args.Email),
                new Claim(ClaimTypes.UserData, args.UserId.ToString()),
                new Claim("MemberSince", Convert.ToDateTime(args.DateCreated).Year.ToString()),

                // Role
                new Claim(ClaimTypes.Role, args.Role.Name),
            }, "MyCookieMiddlewareInstance");

            ClaimsPrincipal principal = new ClaimsPrincipal();

            principal.AddIdentity(ci);

            await HttpContext.SignInAsync("MyCookieMiddlewareInstance", principal);
        }
示例#10
0
        public int Add(EF.User entity)
        {
            entity.Code = entity.Code.ToSafetyString().ToUpper();
            List <EF.KPILevel> kpiLevelList = new List <EF.KPILevel>();

            try
            {
                entity.Password = entity.Password.SHA256Hash();
                entity.State    = true;
                entity.IsActive = true;
                _dbContext.Users.Add(entity);

                _dbContext.SaveChanges();

                IEnumerable <KPIViewModel> kpiVM = from kpi in _dbContext.KPIs
                                                   join cat in _dbContext.Categories on kpi.CategoryID equals cat.ID
                                                   select new KPIViewModel
                {
                    KPIID = kpi.ID,
                };
                foreach (var kpi in kpiVM)
                {
                    var kpilevel = new EF.KPILevel();
                    kpilevel.LevelID = entity.ID;
                    kpilevel.KPIID   = kpi.KPIID;
                    kpiLevelList.Add(kpilevel);
                }

                _dbContext.KPILevels.AddRange(kpiLevelList);
                _dbContext.SaveChanges();

                return(1);
            }
            catch
            {
                return(0);
            }
        }
        public async Task <IActionResult> SignUpWithPassword(EF.User args)
        {
            try
            {
                var configs = (ViewBag.Settings as List <EF.Setting>).Where(x => x.Group == "Config");
                var roleId  = Convert.ToInt16(configs.First(x => x.Key == "Default RoleId").Value);
                var role    = (await new BLL.Role(unitOfWork).GetById(roleId)).Name;

                if (!ModelState.IsValid)
                {
                    return(View("SignUp", args));
                }

                // Asign role
                args.RoleId = roleId;
                args.Role   = new EF.Role {
                    RoleId = roleId, Name = role
                };

                // Add user
                args.DateCreated = DateTime.Now;
                await new BLL.User(unitOfWork).Add(args);

                await AddClaimsAndSignIn(args);

                return(RedirectToAction("Index", "Main"));
            }
            catch (DbUpdateException ex)
            {
                ModelState.AddModelError(string.Empty, "Entry is causing conflict or already exists");
                return(View("SignUp", args));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
示例#12
0
 public async Task <IActionResult> Index(EF.User args)
 {
     ViewData["Title"] = "Users";
     ViewBag.Data      = await new BLL.User(unitOfWork).Find(args);
     return(View());
 }
        public async Task <IActionResult> ForgotPassword(EF.User args)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            var user = await new BLL.User(unitOfWork).Get(new EF.User {
                Email = args.Email
            });

            if (user == null || user.DateInactive != null)
            {
                ModelState.AddModelError(string.Empty, "Unable to validate your email");
                return(View());
            }

            // Generate ConfirmationCode
            Guid   g          = Guid.NewGuid();
            string guidString = Convert.ToBase64String(g.ToByteArray());

            guidString = guidString.Replace("=", "");
            guidString = guidString.Replace("+", "");

            user.ConfirmationCode   = guidString;
            user.ConfirmationExpiry = DateTime.Now.AddHours(12);

            // Update confirmation
            await new BLL.User(unitOfWork).Edit(user);

            var smtpSettings     = (ViewBag.Settings as List <EF.Setting>).Where(x => x.Group == "Smtp");
            var smtpDisplayName  = smtpSettings.First(x => x.Key == "Smtp display name").Value;
            var smtpDisplayEmail = smtpSettings.First(x => x.Key == "Smtp display email").Value;
            var smtpServer       = smtpSettings.First(x => x.Key == "Smtp server").Value;
            var smtpPort         = smtpSettings.First(x => x.Key == "Smtp port").Value;
            var smtpUseSsl       = smtpSettings.First(x => x.Key == "Smtp use ssl").Value;
            var smtpUser         = smtpSettings.First(x => x.Key == "Smtp user").Value;
            var smtpPwd          = smtpSettings.First(x => x.Key == "Smtp password").Value;

            // Send email re: password
            var msg = new MimeMessage();

            msg.From.Add(new MailboxAddress(smtpDisplayName, smtpDisplayEmail));
            msg.To.Add(new MailboxAddress("", args.Email));
            msg.Subject = "Your password";
            var bodyBuilder = new BodyBuilder();

            bodyBuilder.HtmlBody = "Click the link below to change your password:<br/>http://" + Request.Host.Value + "/authorization/confirmation/?userid=" + user.UserId + "&code=" + user.ConfirmationCode;
            msg.Body             = bodyBuilder.ToMessageBody();

            using (var client = new SmtpClient())
            {
                await client.ConnectAsync(smtpServer, Convert.ToInt32(smtpPort), Convert.ToBoolean(smtpUseSsl));

                // Note: only needed if the SMTP server requires authentication
                await client.AuthenticateAsync(smtpUser, smtpPwd);

                await client.SendAsync(msg);

                await client.DisconnectAsync(true);
            }

            TempData["notice"] = "Thank you. Please check your email for confirmation.";
            return(Redirect("~/SignIn"));
        }
 public async Task <IActionResult> SignUpWithPasswordAndWithReCaptcha(EF.User args)
 {
     return(await SignUpWithPassword(args));
 }