/// <summary> /// Instagrams the callback. /// </summary> /// <returns>the Instagram Call back</returns> public ActionResult InstagramCallback() { if (!string.IsNullOrEmpty(Request.QueryString["code"])) { string code = Request.QueryString["code"]; try { NameValueCollection parameters = new NameValueCollection(); parameters.Add("client_id", Util.GetConfigValue("AppInstagramID", string.Empty)); parameters.Add("client_secret", Util.GetConfigValue("AppInstagramSecret", string.Empty)); parameters.Add("grant_type", "authorization_code"); parameters.Add("redirect_uri", Util.GetConfigValue("AppIstagramReturn", string.Empty)); parameters.Add("code", code); WebClient client = new WebClient(); var result = client.UploadValues("https://api.instagram.com/oauth/access_token", "POST", parameters); var response = System.Text.Encoding.Default.GetString(result); //// deserializing nested JSON string to object var javascriptResult = (JObject)JsonConvert.DeserializeObject(response); string accessToken = (string)javascriptResult["access_token"]; string id = (string)javascriptResult["user"]["id"]; string username_client = (string)javascriptResult["user"]["username"]; string fullname_client = (string)javascriptResult["user"]["full_name"]; ////This code register id and access token to get on client side ////Page.ClientScript.RegisterStartupScript(this.GetType(), "GetToken", "<script>var instagramaccessid=\"" + @"" + id + "" + "\"; var instagramaccesstoken=\"" + @"" + accessToken + "" + "\";</script>"); UserModels sv = new UserModels(); C_User it = new C_User(); C_UserInfo iit = new C_UserInfo(); it = sv.GetUserbyUserName(username_client); if (it == null) { it = new C_User(); string rt = string.Empty; bool infors = false; it.Username = username_client; it.Role = 2; it.Password = MD5Extend.EncodePassword("123456"); it.Online = true; it.IP = HttpContext.Request.ServerVariables["REMOTE_ADDR"].ToString(); it.LastLogin = DateTime.Now; rt = sv.Add_User(it); iit.FName = fullname_client; iit.Email = username_client; ////iit.LName = middlename; iit.DateRegister = DateTime.Now; ////iit.EventId = string.IsNullOrEmpty(ids) != false ? int.Parse(ids) : 0; iit.Note = "Instagram login"; infors = sv.Add_User_Infor(iit); if (rt.Length > 0 && infors == true) { ////register_view.message = Lang.strMessageLogin; this.Session["acc"] = iit.FName; this.Session["mem"] = rt; this.Session["role"] = it.Role.ToString(); this.SetAccountCookies(iit.FName, rt, it.Role.ToString(), 6); } } else { iit = sv.GetUserInforByEmail(username_client); this.Session["acc"] = iit.FName; this.Session["mem"] = it.Username; this.Session["role"] = it.Role.ToString(); this.SetAccountCookies(iit.FName, it.Username, it.Role.ToString(), 6); it.Online = true; it.IP = HttpContext.Request.ServerVariables["REMOTE_ADDR"].ToString(); it.LastLogin = DateTime.Now; sv.Update_User(it); } if (this.Session["Instagram_return"] != null && Url.IsLocalUrl(this.Session["Instagram_return"].ToString())) { return(this.Redirect(this.Session["Instagram_return"].ToString())); } else { return(this.RedirectToAction("index", "home")); } } catch (Exception) { throw; } } else { return(this.RedirectToAction("index", "home")); } }
/// <summary> /// Google the callback. /// </summary> /// <returns>Google Call back</returns> public ActionResult GoogleCallback() { GoogleConnect.ClientId = Util.GetConfigValue("AppGoogleID", string.Empty); GoogleConnect.ClientSecret = Util.GetConfigValue("AppGoogleSecret", string.Empty); if (!string.IsNullOrEmpty(Request.QueryString["code"])) { string code = Request.QueryString["code"]; string json = GoogleConnect.Fetch("me", code); GoogleProfile profile = new JavaScriptSerializer().Deserialize <GoogleProfile>(json); UserModels sv = new UserModels(); C_User it = new C_User(); C_UserInfo iit = new C_UserInfo(); it = sv.GetUserbyUserName(profile.Emails.Find(email => email.Type == "account").Value); if (it == null) { it = new C_User(); string rt = string.Empty; bool infors = false; it.Username = profile.Emails.Find(email => email.Type == "account").Value; it.Role = 2; it.Password = MD5Extend.EncodePassword("123456"); it.Online = true; it.IP = HttpContext.Request.ServerVariables["REMOTE_ADDR"].ToString(); it.LastLogin = DateTime.Now; rt = sv.Add_User(it); iit.FName = profile.DisplayName; iit.Email = profile.Emails.Find(email => email.Type == "account").Value; ////iit.LName = middlename; iit.DateRegister = DateTime.Now; ////iit.EventId = string.IsNullOrEmpty(ids) != false ? int.Parse(ids) : 0; iit.Note = "Google login"; infors = sv.Add_User_Infor(iit); if (rt.Length > 0 && infors == true) { ////register_view.message = Lang.strMessageLogin; this.Session["acc"] = iit.FName; this.Session["mem"] = rt; this.Session["role"] = it.Role.ToString(); this.SetAccountCookies(iit.FName, rt, it.Role.ToString(), 6); } } else { iit = sv.GetUserInforByEmail(profile.Emails.Find(email => email.Type == "account").Value); this.Session["acc"] = iit.FName; this.Session["mem"] = it.Username; this.Session["role"] = it.Role.ToString(); this.SetAccountCookies(iit.FName, it.Username, it.Role.ToString(), 6); it.Online = true; it.IP = HttpContext.Request.ServerVariables["REMOTE_ADDR"].ToString(); it.LastLogin = DateTime.Now; sv.Update_User(it); } ////lblId.Text = profile.Id; ////lblName.Text = profile.DisplayName; ////lblEmail.Text = profile.Emails.Find(email => email.Type == "account").Value; ////lblGender.Text = profile.Gender; ////lblType.Text = profile.ObjectType; ////ProfileImage.ImageUrl = profile.Image.Url; if (Request.QueryString["error"] == "access_denied") { ////login fail logger.Error("Error requesting OAuth user data from Google:" + Request.QueryString["error"]); } if (this.Session["Google_return"] != null && Url.IsLocalUrl(this.Session["Google_return"].ToString())) { return(this.Redirect(Session["Google_return"].ToString())); } else { return(this.RedirectToAction("index", "home")); } } else { return(this.RedirectToAction("index", "home")); } }
public ActionResult Register(FormCollection collection) { var register_view = new Register_view(); this.TryUpdateModel(register_view); UserModels sv = new UserModels(); C_User it = new C_User(); C_UserInfo iit = new C_UserInfo(); register_view.Parent_action = HttpContext.Request.RequestContext.RouteData.Values["action"].ToString(); register_view.Parent_controller = HttpContext.Request.RequestContext.RouteData.Values["controller"].ToString(); if (HttpContext.Session["captchastring"] != null && register_view.Code == HttpContext.Session["captchastring"].ToString()) { if (register_view.Password != register_view.Confirm_password) { register_view.Message = App_GlobalResources.Lang.messConfirmPassword; return(this.PartialView("../page/register", register_view)); } if (sv.GetUserbyUserName(register_view.Email) != null) { register_view.Message = App_GlobalResources.Lang.messExistsEmail; return(this.PartialView("../page/register", register_view)); } string rt = string.Empty; bool infors = false; it.Username = register_view.Email; it.Role = 2; it.Password = MD5Extend.EncodePassword(register_view.Password); it.IP = "not login"; it.Online = true; it.LastLogin = DateTime.Now; rt = sv.Add_User(it); iit.FName = register_view.FirstName; iit.Email = register_view.Email; infors = sv.Add_User_Infor(iit); if (rt.Length > 0 && infors == true) { register_view.Message = App_GlobalResources.Lang.strMessageLogin; this.Session["acc"] = register_view.Email; this.Session["mem"] = rt; this.Session["role"] = it.Role.ToString(); this.SetAccountCookies(register_view.Email, rt, it.Role.ToString(), 6); if (!string.IsNullOrEmpty(register_view.Return_url)) { return(this.Redirect(register_view.Return_url)); } else { return(this.RedirectToAction("index", "home")); } } else { register_view.Message = App_GlobalResources.Lang.messErrorRegister; return(this.PartialView("../page/register", register_view)); } } else { register_view.Message = App_GlobalResources.Lang.messCaptcha; return(this.PartialView("../page/register", register_view)); } }
/// <summary> /// Facebook the callback. /// </summary> /// <param name="code">The code.</param> /// <returns>Facebook Call back</returns> public ActionResult FacebookCallback(string code) { var fb = new Facebook.FacebookClient(); dynamic result = fb.Post("oauth/access_token", new { client_id = Util.GetConfigValue("AppFaceBookID", string.Empty), client_secret = Util.GetConfigValue("AppFacebookSecret", string.Empty), redirect_uri = this.RedirectUri.AbsoluteUri, code = code }); var accessToken = result.access_token; //// Store the access token in the session for farther use this.Session["AccessToken"] = accessToken; //// update the facebook client with the access token so //// we can make requests on behalf of the user fb.AccessToken = accessToken; //// Get the user's information dynamic me = fb.Get("me?fields=first_name,middle_name,last_name,id,email"); string email = me.email; string firstname = me.first_name; string middlename = me.middle_name; string lastname = me.last_name; string ids = me.id; //// Set the auth cookie ////FormsAuthentication.SetAuthCookie(email, false); UserModels sv = new UserModels(); C_User it = new C_User(); C_UserInfo iit = new C_UserInfo(); it = sv.GetUserbyUserName(email); if (it == null) { it = new C_User(); string rt = string.Empty; bool infors = false; it.Username = email; it.Role = 2; it.Password = MD5Extend.EncodePassword("123456"); it.Online = true; it.IP = HttpContext.Request.ServerVariables["REMOTE_ADDR"].ToString(); it.LastLogin = DateTime.Now; rt = sv.Add_User(it); iit.FName = firstname; iit.Email = email; iit.LName = middlename; iit.DateRegister = DateTime.Now; iit.EventId = string.IsNullOrEmpty(ids) != false?int.Parse(ids) : 0; iit.Note = "Facebook login"; infors = sv.Add_User_Infor(iit); if (rt.Length > 0 && infors == true) { ////register_view.message = Lang.strMessageLogin; this.Session["acc"] = iit.FName; this.Session["mem"] = rt; this.Session["role"] = it.Role.ToString(); this.SetAccountCookies(iit.FName, rt, it.Role.ToString(), 6); } } else { iit = sv.GetUserInforByEmail(email); this.Session["acc"] = iit.FName; this.Session["mem"] = it.Username; this.Session["role"] = it.Role.ToString(); this.SetAccountCookies(iit.FName, it.Username, it.Role.ToString(), 6); it.Online = true; it.IP = HttpContext.Request.ServerVariables["REMOTE_ADDR"].ToString(); it.LastLogin = DateTime.Now; sv.Update_User(it); } if (this.Session["FaceBook_return"] != null && Url.IsLocalUrl(this.Session["FaceBook_return"].ToString())) { return(this.Redirect(Session["FaceBook_return"].ToString())); } else { return(this.RedirectToAction("index", "home")); } }
public ActionResult Change_user(FormCollection collection) { UserModels userModels = new UserModels(); C_User cuser = null; StringBuilder sb = new StringBuilder(); var user_view = new Web.Areas.Admin.ViewModels.User_view(); this.TryUpdateModel(user_view); string rt = string.Empty; bool is_valid = true; if (!string.IsNullOrEmpty(user_view.Username)) { cuser = userModels.GetUserbyUserName(user_view.Username); } ////validation server if (string.IsNullOrEmpty(user_view.Username)) { is_valid = false; if (user_view.Message == string.Empty) { user_view.Message = "Bạn phải nhập User Name"; } } ////UserName duplicate Admin if (user_view.Username == "Admin") { is_valid = false; if (user_view.Message == string.Empty) { user_view.Message = "Bạn không thể tạo Admin"; } } ////Link tab sb.Append("<li><a href=\"" + Url.Action("index", "dashboard", new { act = "list_user", ctrl = "adminUser", type = user_view.Type, page = "1" }) + "\"><span><span>Danh sách Admin</span></span></a></li>"); if (!string.IsNullOrEmpty(user_view.Username) && user_view.Type_act == CommonGlobal.Edit) { sb.Append("<li class=\"active\"><a href=\"#\"><span><span>" + cuser.Username + "</span></span></a></li>"); } else { sb.Append("<li class=\"active\"><a href=\"#\"><span><span>Thêm mới</span></span></a></li>"); ////validation password if (string.IsNullOrEmpty(user_view.Password)) { is_valid = false; if (user_view.Message == string.Empty) { user_view.Message = "Bạn phải nhập mật khẩu"; } } if (string.IsNullOrEmpty(user_view.ConfirmPassword)) { is_valid = false; if (user_view.Message == string.Empty) { user_view.Message = "Bạn phải xác nhận mật khẩu"; } } if (user_view.Password != user_view.ConfirmPassword) { is_valid = false; if (user_view.Message == string.Empty) { user_view.Message = "Bạn phải xác nhận mật khẩu"; } } } sb.Append("<li><a href=\"" + Url.Action("index", "dashboard", new { act = "list_user_permission", ctrl = "adminUser", type = user_view.Type, user_name = user_view.Username, page = "1" }) + "\"><span><span>Permission</span></span></a></li>"); user_view.Html_link_tab = sb.ToString(); if (!is_valid) { return(this.PartialView("../control/change_user", user_view)); } cuser = new C_User(); cuser.Username = user_view.Username; cuser.Online = user_view.Online; if (cuser.Online == true) { user_view.Online = true; user_view.Online_text = "checked='checked'"; } else { user_view.Online = false; user_view.Online_text = string.Empty; } if (!string.IsNullOrEmpty(user_view.Username) && user_view.Type_act == CommonGlobal.Edit) { rt = userModels.Update_User(cuser); } else { cuser.Role = 1; cuser.Password = MD5Extend.EncodePassword(user_view.Password); cuser.IP = "not login"; cuser.Online = true; cuser.LastLogin = null; rt = userModels.Add_User(cuser); } if (rt.Length > 0) { user_view.Message = "Cập nhật thành công!"; user_view.Username = rt; user_view.Type_act = CommonGlobal.Edit; } else { user_view.Message = "Cập nhật không thành công!"; } return(this.PartialView("../control/change_user", user_view)); }