示例#1
0
        public static void HandleOpenIdProviderResponse(ref string reload)
        {
            string UserName = "******";
            string Email = " ";
            string FirstName = " ";
            string LastName = " ";
            string City = " ";
            string Phone = " ";
            string FullName = "";
            string day = "";
            string month = "";
            string year = "";
            string gender = "";
            string img = "";

            using (var openid = new OpenIdAjaxRelyingParty())
            {

                var response = openid.GetResponse();

                if (response == null) return;
                switch (response.Status)
                {
                    case AuthenticationStatus.Authenticated:
                        var fetchResponse = response.GetExtension<FetchResponse>();
                        //bo sesssion     HttpContext.Current.S-ession["FetchRespose"] = fetchResponse;
                        Email = fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email);
                        Phone = fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Phone.Mobile);
                        FirstName = fetchResponse.GetAttributeValue(WellKnownAttributes.Name.First);
                        FullName = fetchResponse.GetAttributeValue(WellKnownAttributes.Name.FullName);
                        LastName = fetchResponse.GetAttributeValue(WellKnownAttributes.Name.Last);
                        day = fetchResponse.GetAttributeValue(WellKnownAttributes.BirthDate.DayOfMonth);
                        month = fetchResponse.GetAttributeValue(WellKnownAttributes.BirthDate.Month);
                        year = fetchResponse.GetAttributeValue(WellKnownAttributes.BirthDate.Year);
                        City = fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.HomeAddress.City);
                        gender = fetchResponse.GetAttributeValue(WellKnownAttributes.Person.Gender);
                        img = fetchResponse.GetAttributeValue(WellKnownAttributes.Media.Images.Aspect11);
                        // FormsAuthentication.RedirectFromLoginPage(response.ClaimedIdentifier, false);

                        Account account = new Account();

                        Account acctemp = AccountDA.SelectByEmail(Email);
                        int random = 0;
                        if (acctemp == null)
                        {
                            account.Username = Email.Substring(0, Email.IndexOf('@')).Trim();
                            if (AccountDA.SelectByUsername(account.Username) != null)
                            {
                                Random r = new Random(DateTime.Now.Second);
                                random = r.Next(100, 999);
                                account.Username = account.Username + random.ToString();

                            }
                            account.Email = Email.Trim();
                            //account.Password = hashedPassword;
                            int pas = 0;
                            List<int> list = new List<int>();
                            string ps = "";
                            Random ra = new Random(DateTime.Now.Second);
                            pas = ra.Next(100000000, 999999999);
                            for (int i = 65; i <= 90; i++)
                            {
                                list.Add(i);
                            }

                            for (int i = 97; i <= 122; i++)
                            {
                                list.Add(i);

                            }
                            Random rs = new Random(DateTime.Now.Second);
                            for (int i = 0; i < 3; i++)
                            {

                                ps += (char)list[rs.Next(0, list.Count)];
                            }

                            string password = pas.ToString() + ps;
                            account.Password = Encryptor.Encrypt(password);

                            if (Phone != null) account.PhoneNumber = Phone;// Phonetxt.Text;

                            if (City != null) account.City = City;

                            if (day != null && month != null && year != null)
                            {
                                DateTime date = new DateTime(int.Parse(year), int.Parse(month), int.Parse(day));
                                account.Birthday = date;

                            }
                            if (gender != null) account.Gender = gender;

                            if (img != null) account.Avatar = img;

                            account.Company = "Bí Mật";// txtOrganization.Text;
                            account.School = "Bí Mật";// txtSchool.Text;
                            if (FirstName != null && LastName != null)
                            {
                                account.FullName = FirstName + " " + LastName;//txtFirstName.Text;
                                //account.LastName = LastName;//txtLastName.Text;
                            }
                            else
                            {
                                if (FullName != null)
                                {

                                    account.LastName = FullName;
                                }
                                else
                                {
                                    account.FirstName = "";
                                    account.LastName = "";
                                }

                            }

                            account.IP = HttpContext.Current.Request.UserHostAddress.ToString();
                            account.Active = false;

                            string uid = UserName.Trim();

                            long accountID = AccountDA.Insert(account);

                            //dat role default la member luon
                            AccountInRole air = new AccountInRole();
                            air.AccountID = accountID;
                            air.RoleID = AccountRoleDA.SelectByRoleName(RolesAccess.Member).ID;
                            AccountInRoleDA.Insert(air);

                            //success --> notice
                            //  HttpContext.Current.S-ession["xusername"] = account.Username.ToUpper();
                            //MultiView1.SetActiveView(View2);

                            //dang ky xong --> cho tu dong dang nhap luon? chac la minh se cho chay qua trang dang nhap truoc hen

                            // HttpContext.Current.S-ession["first_login"] = true;
                            HttpCookie fist_login = new HttpCookie("first_login", "true");

                            HttpContext.Current.Response.Cookies.Add(fist_login);

                            HttpCookie new_user = new HttpCookie("new_user", account.Username);
                            HttpContext.Current.Response.Cookies.Add(new_user);
                            //   Response.Redirect("http://bit.ly/dang_nhap_pro");//LinkBuilder.SignupSuccess());
                            string goal = HttpContext.Current.Request.Form["txtgoal"];

                            //Send Mail To User After registration

                            string content = "<div style='font-family:arial;color:#333333;'><strong>Gửi bạn " + account.Username + ",</strong>"
                                + "<br/>"
                                 + "<br/>"
                                + "Chào mừng bạn đã đăng ký tài khoản Try2Success thành công."
                                + "<br/>"
                                //+ "Hiện tại tài khoản của bạn chưa được kich hoạt, để kích hoạt tài khoản, bạn soạn tin SMS với cú pháp: <b>REG Try2Success " + uid + " GỬI 8377</b><br />"
                                //+ "Ví dụ:Username của bạn là vitcon thì bạn soạn: REG Try2Success vitcon sau đó gửi đến số 8377. Chúc bạn thành công! <br />"
                                 + "<br/>"
                                + "Bạn hãy thường xuyên ghé thăm website <strong> <a style='text-decoration:underline;' href='www.try2sucess.com'>www.try2sucess.com</a> </strong> học hỏi chia sẻ để phát triển bản thân mỗi ngày nhé!"

                                + "<br/>"
                                 + "<br/>"
                                + "<strong>Để truy cập website bạn có thể sử dụng một trong hai cách sau:</strong>"
                                + "<br/>"
                                + "<br/>"
                                + "<span style='color:#006600;'><strong>1. Truy cập nhanh, thuận tiện, không cần mật khẩu:</strong></span> Ấn vào hình Google/Yahoo trong cửa sổ đăng nhập. Sau đó ấn <span style='color:#006600;'>\"Đồng ý/Allow\"</span>"
                                + "<br/>"
                                + "<br/>"
                                + "<span style='color:#006600;'><strong>2. Truy cập bằng tài khoản Try2Success ID</strong></span>"
                                + "<ul style='list-style-type:circle; margin-left:20px;'>"
                                + "<li>Tài khoản:<strong><a style='text-decoration:underline;' href='mailto:" + account.Email + "'>" + account.Email + "</a></strong></li>"
                                + "<li>Mật khẩu: <strong style='color:#cc0000'>" + password + "</strong></li>"

                                + "</ul>"
                                + "<div style='border-width:1px;border-style:solid;background-color:rgb(105, 167, 78);padding:4px 10px 5px;border-top:1px solid rgb(149, 191, 130);width:160px;margin-left:60px;'> <a target='_blank' href='" + LinkBuilder.getLinkProfile(account) + "/setting' style='color:rgb(255, 255, 255);text-decoration:none;font-weight:bold;font-size:13px'>Thay mật khẩu mặc định</a></div>"
                                // + "<table cellspacing='0' cellpadding='0' style='border-collapse: collapse;'><tbody><tr><td style='padding:4px 10px 5px;border-top:1px solid rgb(149, 191, 130)'><a target='_blank' href='" + account.getAuthorLink() + "/setting' style='color:rgb(255, 255, 255);text-decoration:none;font-weight:bold;font-size:13px'>Để đổi mật khẩu</a></td></tr></tbody></table>"
                                + "<br/>"
                                + "<strong>Xin cảm ơn và chúc một ngày vui vẻ</strong>"

                            + "</div>";

                            AccountInRoleCollection rl = AccountInRoleDA.SelectByAccountID(account.ID);
                            string role = "";
                            foreach (AccountInRole r in rl)
                            {
                                role += (AccountRoleDA.SelectByID(r.RoleID)).Name + ",";
                            }

                            //write authetication information into cookies
                            // trong hàm này la tao mới user tai login bang openid
                            writeCookie(account, role, true);
                            reload = "<script type='text/javascript'>window.location.href=window.location.href</script>";

                            try
                            {

                                SendMail.sendMail(content, "[Try2Success] Chào mừng bạn đến với gia đình Try2Success", "*****@*****.**", "Try2Success Admin", account.Email, account.FirstName + account.LastName);
                            }
                            catch (Exception)
                            {

                            }

                            // Response.Redirect("http://www.Try2Success.net"+Request.RawUrl);

                        }
                        else
                        {
                            //  Role//doan dang nhap o dau
                            AccountInRoleCollection rl = AccountInRoleDA.SelectByAccountID(acctemp.ID);
                            string role = "";
                            foreach (AccountInRole r in rl)
                            {
                                role += (AccountRoleDA.SelectByID(r.RoleID)).Name + ",";
                            }

                            //write authetication information into cookies
                            writeCookie(acctemp, role, true);
                            reload = "<script type='text/javascript'>window.location.href=window.location.href</script>";
                            // Response.Redirect("http://www.Try2Success.net"+Request.RawUrl);

                            //  AfterLoggedIn(acctemp);

                        }
                        break;

                }

            }
        }
示例#2
0
 public OpenIdAjaxRelyingParty(DotNetOpenAuth.OpenId.RelyingParty.OpenIdAjaxRelyingParty actual)
 {
     this.actual = actual;
 }