public async Task <IActionResult> Register(RegisterModel model)
        {
            if (!Captcha.CheckCaptcha(Request))
            {
                ModelState.AddModelError("Captcha", "Captcha is invalid.");
                return(View(model));
            }
            if (ModelState.IsValid)
            {
                // Check if user with the same email already exists in DB
                User user = await db.Users.FirstOrDefaultAsync(u => u.Email == model.Email);

                if (user == null)
                {
                    var hashedPassword = Security.GetHashedPassword(model.Password);

                    // Get developer RoleId
                    var roleId = db.UserRoles.FirstOrDefault(x => x.Name == "Developer").Id;
                    // Add new user to DB
                    db.Users.Add(new User {
                        Email = model.Email, Password = hashedPassword, UserId = Guid.NewGuid().ToString(), RoleId = roleId, Name = model.Name
                    });
                    await db.SaveChangesAsync();

                    return(RedirectToAction(nameof(AccountController.Login), "Account"));
                }
                else
                {
                    ModelState.AddModelError("Email", "User with the same email already exists");
                }
            }
            return(View(model));
        }
Exemplo n.º 2
0
        public void Submit()
        {
            if (IsAjax)
            {
                if (IsPost)
                {
                    if (!IsWap)
                    {
                        if (PassportSection.GetSection().LoginWithCaptcha)
                        {
                            if (!Captcha.CheckCaptcha("login", Request.Form["Captcha"]))
                            {
                                SetResult((int)M.LoginStatus.CaptchaError);
                                return;
                            }
                        }
                    }
                    int           errCount;
                    M.Member      member;
                    string        name   = Request.Form["UserName"];
                    string        pwd    = Request.Form["Password"];
                    M.LoginStatus status = M.Member.Login(DataSource, name, pwd, ClientIp, out errCount, out member);
                    if (status == M.LoginStatus.Success)
                    {
                        Web.PassportAuthentication.SetAuthCookie(true, false, member);
                        OnLogined(member.Id);

                        HttpCookie loginCookie = new HttpCookie("UserName");
                        string     check       = Request.Form["remember"];
                        if (check == "true")
                        {
                            loginCookie.Values.Add("UName", name);
                            loginCookie.Expires = DateTime.Now.AddYears(1);
                            Response.SetCookie(loginCookie);
                        }
                        else
                        {
                            loginCookie.Values.Add("UName", "");
                            loginCookie.Expires = DateTime.Now.AddYears(1);
                            Response.SetCookie(loginCookie);
                        }
                    }
                    SetResult((int)status, errCount);
                }
                else
                {
                    NotFound();
                }
            }
            else
            {
                NotFound();
            }
        }
Exemplo n.º 3
0
        public static bool Sms(string name, int type, DataSource ds)
        {
            try
            {
                PassportSection section = PassportSection.GetSection();
                if (!section.VerifyMobile)
                {
                    throw new Exception();
                }

                HttpRequest Request = HttpContext.Current.Request;
                string      captcha = Request.Form["Captcha"];
                if (!string.IsNullOrEmpty(captcha))
                {
                    if (!Captcha.CheckCaptcha(Request.Form["CaptchaName"], captcha))
                    {
                        throw new Exception();
                    }
                }

                long       mobile   = long.Parse(Request.Form["Mobile"]);
                int        timespan = SMSCaptchaSection.GetSection().TimeSpan;
                MobileHash hash     = MobileHash.Create(ds, mobile, type, timespan);
                if (hash == null)
                {
                    throw new Exception();
                }

                string     md5 = string.Concat(Request.UserHostAddress, "\r\n", Request.UserAgent).MD5();
                StringHash sh  = StringHash.Create(ds, md5, StringHash.SmsHash, timespan);
                if (sh == null)
                {
                    throw new Exception();
                }

                SmsTemplate temp = SmsTemplate.GetByName(ds, SmsTemplate.Register);
                if (temp.Type == SmsTemplateType.Template)
                {
                    SendTemplateImpl(name, mobile, temp.Content, ds, hash.Hash);
                }
                else
                {
                    SendImpl(name, mobile, temp.Content, ds, hash.Hash);
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpRequestBase request = filterContext.RequestContext.HttpContext.Request;
            Captcha         captcha = new Captcha();

            captcha.CaptchaModel.AlwaysShow = this.AlwaysShow;
            captcha.CaptchaModel.ErrorCount = this.ErrorCount;
            if (captcha.Enabled())
            {
                var InputCaptcha      = request.Form["CaptchaCode"];
                var CaptchaSessionKey = request.Form["CKey"];
                if (!captcha.CheckCaptcha(InputCaptcha, CaptchaSessionKey))
                {
                    ((Controller)filterContext.Controller).ModelState.AddModelError("CaptchaError", captcha.CaptchaModel.AlertMessage.Message);
                }
            }
        }
Exemplo n.º 5
0
 public void CheckUser()
 {
     try
     {
         if (!Captcha.CheckCaptcha(Request.Form["CaptchaName"], Request.Form["Captcha"]))
         {
             SetResult((int)M.LoginStatus.CaptchaError);
             return;
         }
         M.Member member = M.Member.Get(DataSource, Request.Form["UserName"]);
         if (member == null)
         {
             SetResult((int)M.LoginStatus.NotFound);
             return;
         }
         SetResult(true, member.Mobile);
     }
     catch (Exception ex)
     {
         SetResult(false, ex.Message);
     }
 }
        public async Task <IActionResult> Login(LoginModel model)
        {
            if (!Captcha.CheckCaptcha(Request))
            {
                ModelState.AddModelError("Captcha", "Captcha is invalid.");
                return(View());
            }

            if (ModelState.IsValid)
            {
                // Get user from DB
                User user = await db.Users.Include(x => x.UserRole).FirstOrDefaultAsync(u => u.Email == model.Email);

                // Check if credentials are correct
                if (user != null && Security.CheckPassword(model.Password, user.Password))
                {
                    // Check if user is approved
                    if (user.IsApproved)
                    {
                        // Check user role (Admin or Dev)
                        await Authenticate(model.Email, user.UserRole.Name == "Admin");

                        return(RedirectToAction(nameof(MonitoringController.Index), "Monitoring"));
                    }
                    else
                    {
                        ModelState.AddModelError("Email", "Your account is not approved yet. Contact your administrator.");
                    }
                }
                else
                {
                    ModelState.AddModelError("Email", "Invalid email or/and Password");
                }
            }
            return(View());
        }
Exemplo n.º 7
0
        public void Submit()
        {
            try
            {
                M.RegisterType  type    = (M.RegisterType) int.Parse(Request.Form["RegisterType"]);
                PassportSection section = PassportSection.GetSection();
                M.Member        member  = DbTable.Load <M.Member>(Request.Form);
                if (type == M.RegisterType.Mobile)
                {
                    if (section.VerifyMobile)
                    {
                        if (!V.MobileHash.Equals(DataSource, member.Mobile, V.MobileHash.Register, Request.Form["SmsCaptcha"]))
                        {
                            SetResult((int)M.LoginStatus.SmsCaptchaError);
                            return;
                        }
                        member.VerMob = true;
                    }
                }
                if (!IsWap)
                {
                    if (section.RegisterWithCaptcha)
                    {
                        if (!Captcha.CheckCaptcha(Request.Form["CaptchaName"], Request.Form["Captcha"]))
                        {
                            SetResult((int)M.LoginStatus.CaptchaError);
                            return;
                        }
                    }
                }
                string password = member.Password;
                if (member.ParentId == 0)
                {
                    bool convertResult = long.TryParse(Request.QueryString["ParentId"], out member.ParentId);
                    if (!convertResult)
                    {
                        member.ParentId = Utility.GetReference(this, DataSource);
                    }
                }
                member.Approved     = section.DefaultApproved;
                member.CreationDate = DateTime.Now;
                DataStatus status = member.Insert(DataSource);
                if (status == DataStatus.Success)
                {
                    int    errCount;
                    string name;
                    switch (type)
                    {
                    case M.RegisterType.Email: name = member.Email; break;

                    case M.RegisterType.Mobile: name = member.Mobile.ToString(); break;

                    default: name = member.Name; break;
                    }
                    M.LoginStatus state = M.Member.Login(DataSource, name, password, ClientIp, out errCount, out member);
                    if (state == M.LoginStatus.Success)
                    {
                        Web.PassportAuthentication.SetAuthCookie(true, false, member);
                    }
                    SetResult((int)state);
                }
                else
                {
                    SetResult((int)status);
                }
            }
            catch (Exception)
            {
                SetResult(false);
            }
        }