public OperationResult GetLineInfo(string code) { var or = new OperationResult(); try { var url = "https://api.line.me/oauth2/v2.1/token"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; NameValueCollection postParams = HttpUtility.ParseQueryString(string.Empty); var values = new Dictionary <string, string> { { "grant_type", "authorization_code" }, { "client_id", $"{WebConfigurationManager.AppSettings["Line_client_id"]}" }, { "client_secret", $"{WebConfigurationManager.AppSettings["Line_client_secret"]}" }, { "code", code }, { "redirect_uri", $"{WebConfigurationManager.AppSettings["WebsiteUrl"]}/Account/LineLogin" } }; foreach (var kvp in values) { postParams.Add(kvp.Key, kvp.Value); } byte[] byteArray = Encoding.UTF8.GetBytes(postParams.ToString()); using (Stream reqStream = request.GetRequestStream()) { reqStream.Write(byteArray, 0, byteArray.Length); } string responseStr = ""; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseStr = sr.ReadToEnd(); } } LineLoginToken tokenObj = JsonConvert.DeserializeObject <LineLoginToken>(responseStr); string id_token = tokenObj.Id_token; var jst = new System.IdentityModel.Tokens.Jwt.JwtSecurityToken(id_token); LineUserProfile user = new LineUserProfile(); user.UserId = jst.Payload.Sub; user.DisplayName = jst.Payload["name"].ToString(); user.PictureUrl = jst.Payload["picture"].ToString(); if (jst.Payload.ContainsKey("email") && !string.IsNullOrEmpty(Convert.ToString(jst.Payload["email"]))) { user.Email = jst.Payload["email"].ToString(); } or.IsSuccessful = true; or.MessageInfo = JsonConvert.SerializeObject(user); } catch (Exception ex) { or.IsSuccessful = false; or.Exception = ex; or.MessageInfo = "發生錯誤"; } return(or); }
public void CreateUserDefinedDataInFavorite(IEnumerable <UserDefinedAll> model, string name, Guid TempGuid) { var index = 0; var result = new OperationResult(); using (var transcation = _context.Database.BeginTransaction()) { try { foreach (var i in model) { var product = new UserDefinedProduct { UserDefinedProductId = Guid.NewGuid(), UserDefinedId = TempGuid, AccountName = name, ServiceItems = i.ServiceItem, RoomType = i.RoomType, Squarefeet = i.Squarefeet, Name = i.Title, Hour = countHour(i.RoomType, i.Squarefeet), Price = Convert.ToDecimal(countHour(i.RoomType, i.Squarefeet)) * 500, CreateTime = DateTime.UtcNow.AddHours(8), CreateUser = name, EditTime = DateTime.UtcNow.AddHours(8), EditUser = name, Index = index }; index++; _repository.Create <UserDefinedProduct>(product); } _context.SaveChanges(); var userfavorite = new UserFavorite { FavoriteId = Guid.NewGuid(), AccountName = name, UserDefinedId = TempGuid, PackageProductId = null, IsPackage = false, IsDelete = false, CreateTime = DateTime.UtcNow.AddHours(8), CreateUser = name, EditTime = DateTime.UtcNow.AddHours(8), EditUser = name, }; _repository.Create <UserFavorite>(userfavorite); _context.SaveChanges(); result.IsSuccessful = true; transcation.Commit(); } catch (Exception ex) { result.IsSuccessful = false; result.Exception = ex; transcation.Rollback(); } } }
public OperationResult RegisterByThirdParty(SocialRegisterViewModel model) { var op = new OperationResult(); if (model.IsIntegrated) { if (IsLoginValid(model.AccountName, model.Password)) { var user = GetUser(model.AccountName, Helpers.ToMD5(model.Password)); user.IsIntegrated = true; user.IsThirdParty = true; user.SocialPlatform = model.SocialPlatform; user.EditTime = DateTime.UtcNow.AddHours(8); user.AccountName = model.AccountName; _repository.Update <Account>(user); _context.SaveChanges(); op.IsSuccessful = true; op.MessageInfo = "註冊成功"; } else { op.IsSuccessful = false; op.MessageInfo = "帳密有誤"; } } else { var account = new RegisterViewModel { Email = model.Email, Address = null, Gender = 3, EmailVerification = true, Name = model.AccountName, ConfirmPassword = "", Password = "", SocialPatform = model.SocialPlatform, Phone = null, IsIntegrated = false, IsThirdParty = true, IsProvidedByThirdParty = model.IsIsProvidedByThirdParty }; if (model.IsIsProvidedByThirdParty) { account.IsProvidedByUser = model.IsIsProvidedByThirdParty; } else { account.IsProvidedByUser = !string.IsNullOrEmpty(model.Email); } var result = CreateAccount(account); if (result.IsSuccessful) { op.IsSuccessful = result.IsSuccessful; op.MessageInfo = "註冊成功"; } else { op.IsSuccessful = result.IsSuccessful; op.MessageInfo = "註冊失敗"; } } return(op); }