Exemplo n.º 1
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.º 2
0
        static PassportAuthentication()
        {
            PassportSection section = PassportSection.GetSection();

            _ivBytes      = Encoding.UTF8.GetBytes(section.CookieIV);
            _keyBytes     = Encoding.UTF8.GetBytes(section.CookieKey);
            _cookieName   = section.CookieName;
            _cookieDomain = section.CookieDomain;
            _maxInvalidPasswordAttempts           = section.MaxInvalidPasswordAttempts;
            _passwordAnswerAttemptLockoutDuration = section.PasswordAnswerAttemptLockoutDuration;
            _level        = section.Level;
            _dataProvider = section.DataProvider;
        }
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 void Reg()
        {
            string target = Request.QueryString["target"];

            if (string.IsNullOrEmpty(target))
            {
                if (Request.UrlReferrer != null)
                {
                    target = Request.UrlReferrer.ToString();
                }
            }
            this["Target"] = target;
            this["Config"] = PassportSection.GetSection();
            this["Sms"]    = SMSCaptchaSection.GetSection();
            Render("reg.html");
        }
Exemplo n.º 5
0
        public void Index()
        {
            string target = Request.QueryString["target"];

            if (string.IsNullOrEmpty(target))
            {
                if (Request.UrlReferrer != null)
                {
                    if (!Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1].EndsWith("logout.html", StringComparison.InvariantCultureIgnoreCase))
                    {
                        target = Request.UrlReferrer.ToString();
                    }
                }
            }
            if (string.IsNullOrEmpty(target))
            {
                target = GetUrl("ucenter");
            }
            if (User != null && User.Identity != null && User.Identity.IsAuthenticated && !User.Identity.IsAdmin)
            {
                Redirect(target);
            }
            else
            {
                if (IsWap)
                {
                    Match m = MicroMessengerRegex.Match(Request.UserAgent);
                    if (m.Success)
                    {
                        M.OAuth2 oa = M.OAuth2.GetById(DataSource, "weixin");
                        if (oa != null)
                        {
                            if (oa.Enabled)
                            {
                                Redirect(string.Concat(GetUrl("/oauth2/login/weixin"), "?target=", HttpUtility.UrlEncode(target)));
                                return;
                            }
                        }
                    }
                }
                HttpCookie Uname = Request.Cookies["UserName"];
                this["UserName"] = Uname != null ? Uname.Values["UName"] : "";
                this["Target"]   = target;
                this["Config"]   = PassportSection.GetSection();
                Render("login.html");
            }
        }
Exemplo n.º 6
0
        public void SendSms(string name)
        {
            try
            {
                PassportSection section = PassportSection.GetSection();
                if (!section.VerifyMobile)
                {
                    throw new Exception();
                }

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

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

                S.SmsTemplate temp = S.SmsTemplate.GetByName(DataSource, S.SmsTemplate.Register);
                if (temp.Type == S.SmsTemplateType.Template)
                {
                    SendTemplateImpl(name, mobile, temp.Content, hash.Hash);
                }
                else
                {
                    SendImpl(name, mobile, temp.Content, hash.Hash);
                }
                SetResult(true);
            }
            catch (Exception)
            {
                SetResult(false);
            }
        }
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);
            }
        }
Exemplo n.º 8
0
 public static void SetReference(Controller ctl, long userId)
 {
     ctl.Request.Cookies[ReferenceCookieName].Value  = userId.ToString();
     ctl.Request.Cookies[ReferenceCookieName].Domain = PassportSection.GetSection().CookieDomain;
 }
Exemplo n.º 9
0
        public void System(string type)
        {
            if (CheckRight())
            {
                if (CheckPost(string.Concat("system_", type), new Action(() =>
                {
                    switch (type)
                    {
                    case "config":
                        this["Config"] = new
                        {
                            Theme = Application.Settings.Theme,
                            UrlMode = Application.Settings.UrlMode.ToString(),
                            Management = Application.Settings.Management,
                            Themes = Application.Settings.GetThemes(),
                            WapDomain = Application.Settings.WapDomain,
                            SubDomain = Application.Settings.SubDomain,
                            DataProvider = Application.Settings.DataProvider,
                            CacheMode = Application.Settings.CacheMode,
                            CacheProvider = Application.Settings.CacheProvider,
                            ResourcesUrl = Application.Settings.ResourcesUrl,
                            PassportUrl = Application.Settings.PassportUrl
                        };
                        this["Conns"] = WebConfigurationManager.ConnectionStrings;
                        break;

                    case "passport":
                        this["Config"] = PassportSection.GetSection();
                        this["Conns"] = WebConfigurationManager.ConnectionStrings;
                        break;

                    case "captcha":
                        this["Config"] = CaptchaSection.GetSection();
                        break;

                    case "smscaptcha":
                        this["Config"] = SMSCaptchaSection.GetSection();
                        break;

                    case "filesystem":
                        this["Config"] = FileSystemSection.GetSection();
                        break;

                    case "email":
                        this["Config"] = (SmtpSection)WebConfigurationManager.GetSection("system.net/mailSettings/smtp");
                        break;

                    case "friendlink":
                        this["Config"] = FriendLinkSection.GetSection();
                        break;

                    case "robots":
                        {
                            string content = string.Empty;
                            try
                            {
                                string file = Server.MapPath("~/robots.txt");
                                content = Encoding.UTF8.GetString(File.ReadAllBytes(file));
                            }
                            catch (Exception) { }
                            this["Robots"] = content;
                        }
                        break;

                    case "sitemap":
                        break;
                    }
                })))
                {
                    switch (type)
                    {
                    case "site":
                    {
                        int           i  = 0;
                        StringBuilder sb = new StringBuilder();
                        foreach (string key in Request.Form.Keys)
                        {
                            if (i++ > 0)
                            {
                                sb.Append(',');
                            }
                            sb.Append(string.Concat("\"", key, "\"=\"", HttpUtility.HtmlEncode(Request.Form[key]), "\""));
                        }
                        try
                        {
                            File.WriteAllText(Server.MapPath(string.Concat(Utility.ConfigDir, "site.config")), string.Concat("$set(Site=array(", sb.ToString(), "))"));
                            SetResult(() =>
                                {
                                    WritePostLog("MOD");
                                });
                        }
                        catch (Exception ex)
                        {
                            SetResult(false, ex.Message);
                        }
                    }
                    break;

                    case "config":
                    {
                        try
                        {
                            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
                            SiteSection ss = SiteSection.GetSection(config);
                            ss.Theme = Request.Form["Theme"];
                            try { ss.UrlMode = (SiteUrlMode)int.Parse(Request.Form["UrlMode"]); }
                            catch (Exception) { ss.UrlMode = SiteUrlMode.Rewrite; }
                            ss.Management   = Request.Form["Management"];
                            ss.WapDomain    = Request.Form["WapDomain"];
                            ss.SubDomain    = Request.Form["SubDomain"];
                            ss.DataProvider = Request.Form["DataProvider"];
                            try { ss.CacheMode = (CacheMode)int.Parse(Request.Form["CacheMode"]); }
                            catch (Exception) { ss.CacheMode = CacheMode.Application; }
                            ss.CacheProvider  = Request.Form["CacheProvider"];
                            ss.ResourcesUrl   = Request.Form["ResourcesUrl"];
                            ss.PassportUrl    = Request.Form["PassportUrl"];
                            ss.WapPassportUrl = Request.Form["WapPassportUrl"];
                            config.Save();
                            SetResult(() =>
                                {
                                    WritePostLog("MOD");
                                });
                        }
                        catch (Exception ex)
                        {
                            SetResult(false, ex.Message);
                        }
                    }
                    break;

                    case "passport":
                    {
                        try
                        {
                            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
                            PassportSection section = PassportSection.GetSection(config);
                            section.CookieName   = Request.Form["CookieName"];
                            section.CookieDomain = Request.Form["CookieDomain"];
                            section.CookieIV     = Request.Form["CookieIV"];
                            section.CookieKey    = Request.Form["CookieKey"];
                            section.MaxInvalidPasswordAttempts           = GetInt32Value(Request.Form["MaxInvalidPasswordAttempts"], Utility.PassportMaxInvalidPasswordAttempts);
                            section.PasswordAnswerAttemptLockoutDuration = GetInt32Value(Request.Form["PasswordAnswerAttemptLockoutDuration"], Utility.PassportPasswordAnswerAttemptLockoutDuration);
                            try { section.Level = (PassportLevel)int.Parse(Request.Form["Level"]); }
                            catch (Exception) { section.Level = PassportLevel.Low; }
                            section.VerifyMail          = GetBooleanValue(Request.Form["VerifyMail"]);
                            section.VerifyMobile        = GetBooleanValue(Request.Form["VerifyMobile"]);
                            section.LoginWithCaptcha    = GetBooleanValue(Request.Form["LoginWithCaptcha"]);
                            section.RegisterWithCaptcha = GetBooleanValue(Request.Form["RegisterWithCaptcha"]);
                            section.DefaultApproved     = GetBooleanValue(Request.Form["DefaultApproved"]);
                            section.DataProvider        = Request.Form["DataProvider"];
                            config.Save();
                            SetResult(() =>
                                {
                                    WritePostLog("MOD");
                                });
                        }
                        catch (Exception ex)
                        {
                            SetResult(false, ex.Message);
                        }
                    }
                    break;

                    case "cache":
                        CacheProvider.Current.Clear();
                        SetResult(() =>
                        {
                            WriteLog("CLEAR CACHE");
                        });
                        break;

                    case "captcha":
                    {
                        try
                        {
                            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
                            CaptchaSection section = CaptchaSection.GetSection(config);
                            section.Chars         = Request.Form["Chars"];
                            section.CookiePrefix  = Request.Form["CookiePrefix"];
                            section.CookieDomain  = Request.Form["CookieDomain"];
                            section.DefaultWidth  = GetInt32Value(Request.Form["DefaultWidth"], Utility.CaptchaDefaultWidth);
                            section.DefaultHeight = GetInt32Value(Request.Form["DefaultHeight"], Utility.CaptchaDefaultHeight);
                            section.DefaultCount  = GetInt32Value(Request.Form["DefaultCount"], Utility.CaptchaDefaultCount);
                            section.Expiration    = GetInt32Value(Request.Form["Expiration"], Utility.CaptchaExpiration);
                            config.Save();
                            SetResult(() =>
                                {
                                    WritePostLog("MOD");
                                });
                        }
                        catch (Exception ex)
                        {
                            SetResult(false, ex.Message);
                        }
                    }
                    break;

                    case "smscaptcha":
                    {
                        try
                        {
                            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
                            SMSCaptchaSection section = SMSCaptchaSection.GetSection(config);
                            section.Chars        = Request.Form["Chars"];
                            section.DefaultCount = GetInt32Value(Request.Form["DefaultCount"], Utility.SMSCaptchaDefaultCount);
                            section.TimeSpan     = GetInt32Value(Request.Form["TimeSpan"], Utility.SMSCaptchaTimeSpan);
                            section.Expiration   = GetInt32Value(Request.Form["Expiration"], Utility.SMSCaptchaExpiration);
                            config.Save();
                            SetResult(() =>
                                {
                                    WritePostLog("MOD");
                                });
                        }
                        catch (Exception ex)
                        {
                            SetResult(false, ex.Message);
                        }
                    }
                    break;

                    case "filesystem":
                    {
                        try
                        {
                            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
                            FileSystemSection section = FileSystemSection.GetSection(config);
                            section.Enable = Types.GetBooleanFromString(Request.Form["Enable"]);
                            section.Path   = Request.Form["Path"];
                            section.Url    = Request.Form["Url"];
                            try { section.Mark = (ImageMarkType)int.Parse(Request.Form["Mark"]); }
                            catch (Exception) { section.Mark = ImageMarkType.None; }
                            section.Text   = Request.Form["Text"];
                            section.Region = (ImageMarkRegion)int.Parse(Request.Form["Region"]);
                            try { section.Width = int.Parse(Request.Form["Width"]); }
                            catch (Exception) { section.Width = 0; }
                            try { section.Height = int.Parse(Request.Form["Height"]); }
                            catch (Exception) { section.Height = 0; }
                            config.Save();
                            SetResult(() =>
                                {
                                    WritePostLog("MOD");
                                });
                        }
                        catch (Exception ex)
                        {
                            SetResult(false, ex.Message);
                        }
                    }
                    break;

                    case "email":
                        try
                        {
                            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
                            SmtpSection section = (SmtpSection)config.GetSection("system.net/mailSettings/smtp");
                            section.From              = Request.Form["UserName"];
                            section.Network.Host      = Request.Form["Host"];
                            section.Network.Port      = GetInt32Value(Request.Form["Port"], 25);
                            section.Network.UserName  = Request.Form["UserName"];
                            section.Network.Password  = Request.Form["Password"];
                            section.Network.EnableSsl = GetBooleanValue(Request.Form["EnableSsl"]);
                            config.Save();
                            SetResult(() =>
                            {
                                WritePostLog("MOD");
                            });
                        }
                        catch (Exception ex)
                        {
                            SetResult(false, ex.Message);
                        }
                        break;

                    case "friendlink":
                    {
                        try
                        {
                            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
                            FriendLinkSection section = FriendLinkSection.GetSection(config);
                            section.Enable   = Types.GetBooleanFromString(Request.Form["Enable"]);
                            section.Approved = Types.GetBooleanFromString(Request.Form["Approved"]);
                            try { section.Mode = (FriendLinkMode)int.Parse(Request.Form["Mode"]); }
                            catch (Exception) { section.Mode = FriendLinkMode.Text; }
                            config.Save();
                            SetResult(() =>
                                {
                                    WritePostLog("MOD");
                                });
                        }
                        catch (Exception ex)
                        {
                            SetResult(false, ex.Message);
                        }
                    }
                    break;

                    case "robots":
                        try
                        {
                            string file = Server.MapPath("~/robots.txt");
                            File.WriteAllBytes(file, Encoding.UTF8.GetBytes(Request.Form["Robots"]));
                            SetResult(() =>
                            {
                                WritePostLog("SUB");
                            });
                        }
                        catch (Exception ex)
                        {
                            SetResult(false, ex.Message);
                        }
                        break;

                    case "sitemap":
                        break;
                    }
                }
            }
        }