public ActionResult FacebookCallback(string redirect) { var fb = new FacebookClient(); FacebookOAuthResult fbResult; if (fb.TryParseOAuthCallbackUrl(Request.Url, out fbResult)) { if (fbResult.IsSuccess) { //pega nome, id, email e 3rd party id do facebook dynamic tokenResult = fb.Post("oauth/access_token", new { client_id = NimbusConfig.FacebookAppId, client_secret = NimbusConfig.FacebookAppSecret, redirect_uri = Request.Url.AbsoluteUri, //eh bizarro mas precisa disso code = fbResult.Code }); string accessToken = tokenResult.access_token; fb.AccessToken = accessToken; dynamic fbUserInfo = fb.Get("me?fields=first_name,last_name,id,email,third_party_id,birthday"); string fbEmail = fbUserInfo.email; //tenta achar o usuario no BD Model.ORM.User nimbusUser; using (var db = DatabaseFactory.OpenDbConnection()) { nimbusUser = db.Where <Model.ORM.User>(u => u.Email == fbEmail).FirstOrDefault(); if (nimbusUser == null) { //usuário sem conta nimbus, cria uma conta pra ele DateTime fbBirthday = Convert.ToDateTime(fbUserInfo.birthday, new DateTimeFormatInfo() { ShortDatePattern = "MM/dd/yyyy" }); string pathAvatar; dynamic fbAvatar = fb.Get("me/picture?redirect=0&type=large"); string fbAvatarUrl = fbAvatar.data.url; //precisa dessas idas e vindas por causa do fbAvatar ser dynamic if (fbAvatar.data.is_silhouette == true) { pathAvatar = "/images/av130x130/person_icon.png"; } else { var req = HttpWebRequest.Create(fbAvatarUrl); var respStream = req.GetResponse().GetResponseStream(); var img = new ImageManipulation(respStream); string thirdpartyid = fbUserInfo.third_party_id; pathAvatar = UserProfileController.UploadAvatar(img, thirdpartyid); } var newUser = new Model.ORM.User() { Email = fbEmail, FirstName = fbUserInfo.first_name, LastName = fbUserInfo.last_name, BirthDate = fbBirthday, AvatarUrl = pathAvatar, Password = "******" + fbUserInfo.id }; db.Insert(newUser); newUser.Id = (int)db.GetLastInsertId(); nimbusUser = newUser; } } //usuário já possui conta Nimbus, entao faz login pra ele Guid token; //Cria token com validade de 7 dias string authToken = Token.GenerateToken( new NSCInfo() { TokenGenerationDate = DateTime.Now.ToUniversalTime(), TokenExpirationDate = DateTime.Now.AddDays(Const.CookieExpiryDays).ToUniversalTime(), UserId = nimbusUser.Id }, out token); //Lembre-se de expirar o cookie também var loginCookie = new HttpCookie("nsc-session", authToken) { Expires = DateTime.Now.AddDays(Const.CookieExpiryDays) }; var modalHowToUseCookie = new HttpCookie("ShowHowToUse", "false") { Expires = DateTime.MaxValue }; //adiciona objeto do usuário logado à sessão Session[Const.UserSession] = DatabaseLogin.GetNimbusPrincipal(nimbusUser); Response.Cookies.Add(loginCookie); Response.Cookies.Add(modalHowToUseCookie); return(Redirect(redirect)); } //fim if (fbResult.IsSuccess) else { //erro no login do face if (fbResult.ErrorReason == "user_denied") { return(Redirect("/login?errormessage=" + Uri.EscapeDataString("É necessário autorizar o login pelo Facebook."))); } else { return(Redirect("/login?errormessage=" + Uri.EscapeDataString("Ocorreu um erro ao fazer login pelo Facebook."))); } } } else { //fim if (fb.TryParseOAuthCallbackUrl //usuario quis brincar com callback do facebook, redirect pra login sem nem mostrar erro return(Redirect("/login")); } }
public static void CreateDatabaseIfNotThere() { var dbFactory = new OrmLiteConnectionFactory (NimbusConfig.DatabaseConnection, SqlServerDialect.Provider); using (var db = dbFactory.OpenDbConnection()) { if (!db.TableExists("Organization")) { using (var trans = db.OpenTransaction()) { //criar tabelas db.CreateTable(false, typeof(Category)); db.CreateTable(false, typeof(ImgTopChannel)); db.CreateTable(false, typeof(Ad)); db.CreateTable(false, typeof(Organization)); db.CreateTable(false, typeof(User)); db.CreateTable(false, typeof(UserAds)); db.CreateTable(false, typeof(Channel)); db.CreateTable(false, typeof(ChannelReported)); db.CreateTable(false, typeof(ChannelUser)); db.CreateTable(false, typeof(OrganizationUser)); db.CreateTable(false, typeof(UserChannelReadLater)); db.CreateTable(false, typeof(Role)); db.CreateTable(false, typeof(OrganizationUser)); // db.CreateTable(false, typeof(RoleOrganization)); //tabela em branco db.CreateTable(false, typeof(Topic)); db.CreateTable(false, typeof(TopicReported)); db.CreateTable(false, typeof(UserExam)); db.CreateTable(false, typeof(RoleTopic)); db.CreateTable(false, typeof(Tag)); db.CreateTable(false, typeof(TagChannel)); db.CreateTable(false, typeof(TagTopic)); db.CreateTable(false, typeof(Comment)); db.CreateTable(false, typeof(CommentReported)); db.CreateTable(false, typeof(Message)); db.CreateTable(false, typeof(UserTopicFavorite)); db.CreateTable(false, typeof(Premium)); db.CreateTable(false, typeof(PremiumUser)); db.CreateTable(false, typeof(Prices)); db.CreateTable(false, typeof(ReceiverMessage)); db.CreateTable(false, typeof(UserInfoPayment)); db.CreateTable(false, typeof(UserLikeTopic)); db.CreateTable(false, typeof(UserReported)); db.CreateTable(false, typeof(ViewByTopic)); db.CreateTable(false, typeof(VoteChannel)); db.CreateTable(false, typeof(Notification <object>)); db.CreateTable(false, typeof(StorageUpload)); db.CreateTable(false, typeof(UserTopicReadLater)); //badge: tabela em branco //userbadge: tabela em branco //log_user: tabela em branco var nimbusorg = new Nimbus.Model.ORM.Organization() { Cname = "www.portalnimbus.com.br", Id = 1, Name = "Portal Nimbus" }; db.Save(nimbusorg); var sysuser = new Model.ORM.User() { Id = 1, FirstName = "System", LastName = "Administrator", Email = "*****@*****.**", Password = new Security.PlaintextPassword("local@adm1").Hash, TOTPKey = Security.OneTimePassword.GenerateSecret(), BirthDate = DateTime.Now, AvatarUrl = "/images/av130x130/person_icon.png" }; db.Save(sysuser); db.Save(new Model.ORM.Category() { Id = 1, ImageUrl = "/images/category/saude.png", Name = "Saúde", }); db.Save(new Model.ORM.Category() { Id = 2, ImageUrl = "/images/category/animacao.png", Name = "Animação", }); db.Save(new Model.ORM.Category() { Id = 3, ImageUrl = "/images/category/artes.png", Name = "Artes", }); db.Save(new Model.ORM.Category() { Id = 4, ImageUrl = "/images/category/artesanato.png", Name = "Artesanato", }); db.Save(new Model.ORM.Category() { Id = 5, ImageUrl = "/images/category/astronomia.png", Name = "Astronomia", }); db.Save(new Model.ORM.Category() { Id = 6, ImageUrl = "/images/category/certificacao.png", Name = "Certificação", }); db.Save(new Model.ORM.Category() { Id = 8, ImageUrl = "/images/category/concursos.png", Name = "Concursos", }); db.Save(new Model.ORM.Category() { Id = 9, ImageUrl = "/images/category/culinaria.png", Name = "Culinária", }); db.Save(new Model.ORM.Category() { Id = 10, ImageUrl = "/images/category/esportes.png", Name = "Esportes", }); db.Save(new Model.ORM.Category() { Id = 11, ImageUrl = "/images/category/filosofia.png", Name = "Filosofia", }); db.Save(new Model.ORM.Category() { Id = 12, ImageUrl = "/images/category/fotografia.png", Name = "Fotografia", }); db.Save(new Model.ORM.Category() { Id = 13, ImageUrl = "/images/category/jardinagem.png", Name = "Jardinagem", }); db.Save(new Model.ORM.Category() { Id = 14, ImageUrl = "/images/category/idiomas.png", Name = "Idiomas", }); db.Save(new Model.ORM.Category() { Id = 15, ImageUrl = "/images/category/literatura.png", Name = "Literatura", }); db.Save(new Model.ORM.Category() { Id = 16, ImageUrl = "/images/category/musica.png", Name = "Música", }); db.Save(new Model.ORM.Category() { Id = 17, ImageUrl = "/images/category/computacao.png", Name = "Computação", }); db.Save(new Model.ORM.Category() { Id = 18, ImageUrl = "/images/category/televisao.png", Name = "Televisão", }); db.Save(new Model.ORM.Category() { Id = 19, ImageUrl = "/images/category/vestibular.png", Name = "Vestibular", }); db.Save(new Model.ORM.Category() { Id = 20, ImageUrl = "/images/category/redacao.png", Name = "Redação", }); db.Save(new Model.ORM.Category() { Id = 21, ImageUrl = "/images/category/ciencias.png", Name = "Ciências", }); db.Save(new Model.ORM.Category() { Id = 22, ImageUrl = "/images/category/biologia.png", Name = "Biologia", }); db.Save(new Model.ORM.Category() { Id = 23, ImageUrl = "/images/category/business.png", Name = "Business", }); db.Save(new Model.ORM.Category() { Id = 24, ImageUrl = "/images/category/empreendedorismo.png", Name = "Empreendedorismo", }); db.Save(new Model.ORM.Category() { Id = 25, ImageUrl = "/images/category/fisica.png", Name = "Física", }); db.Save(new Model.ORM.Category() { Id = 26, ImageUrl = "/images/category/geografia.png", Name = "Geografia", }); db.Save(new Model.ORM.Category() { Id = 27, ImageUrl = "/images/category/historia.png", Name = "História", }); db.Save(new Model.ORM.Category() { Id = 28, ImageUrl = "/images/category/matematica.png", Name = "Matemática", }); db.Save(new Model.ORM.Category() { Id = 29, ImageUrl = "/images/category/portugues.png", Name = "Português", }); db.Save(new Model.ORM.Category() { Id = 30, ImageUrl = "/images/category/quimica.png", Name = "Química", }); trans.Commit(); } } } }