예제 #1
0
        public static string CreateShortURL(string _FullURL, URLShortenerService _Service = URLShortenerService.Google, string _APIKey = null)
        {
            if (_Service != URLShortenerService.Google)
            {
                return("");
            }

            if (_APIKey == null)
            {
                _APIKey = HiddenStrings.GoogleAPIKey;
            }

            try
            {
                string responseText = "";
                var    webRequest   = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=" + _APIKey);
                webRequest.Method      = "POST";
                webRequest.ContentType = "application/json";

                byte[] byteData = UTF8Encoding.UTF8.GetBytes("{\"longUrl\": \"" + _FullURL + "\", \"key\":\"" + _APIKey + "\"}");
                webRequest.ContentLength = byteData.Length;

                var postStream = webRequest.GetRequestStream();
                postStream.Write(byteData, 0, byteData.Length);

                using (var webResponse = webRequest.GetResponse())
                {
                    using (var responseStream = webResponse.GetResponseStream())
                    {
                        using (System.IO.StreamReader reader = new System.IO.StreamReader(responseStream))
                        {
                            responseText = reader.ReadToEnd();
                        }
                    }
                }
                postStream.Close();

                var jsonData = (new JavaScriptSerializer()).Deserialize <Dictionary <string, dynamic> >(responseText);
                if (jsonData.ContainsKey("id") == true && jsonData.ContainsKey("longUrl") == true)
                {
                    if (Uri.Compare(new Uri(((string)jsonData["longUrl"])), new Uri(_FullURL), UriComponents.Query, UriFormat.Unescaped, StringComparison.InvariantCulture) == 0)
                    {
                        return(jsonData["id"]);
                    }
                }
            }
            catch (Exception)
            {
                return("");
            }
            return("");
        }
예제 #2
0
        public static string GetFullURLAsync(string _ShortenedURL, URLShortenerService _Service = URLShortenerService.Google)
        {
            var retValue = "";
            var task     = new System.Threading.Tasks.Task(() => {
                retValue = GetFullURL(_ShortenedURL, _Service);
            });

            task.Start();
            while (task.Wait(50))
            {
            }
            return(retValue);
        }
예제 #3
0
        public static string GetFullURL(string _ShortenedURL, URLShortenerService _Service = URLShortenerService.Google, string _APIKey = null)
        {
            if (_Service != URLShortenerService.Google)
            {
                return("");
            }

            if (_APIKey == null)
            {
                _APIKey = HiddenStrings.GoogleAPIKey;
            }

            if (_ShortenedURL.StartsWith("http://") == false)
            {
                _ShortenedURL = "http://goo.gl/" + _ShortenedURL;
            }
            try
            {
                string responseText = "";
                var    webRequest   = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?shortUrl=" + _ShortenedURL + "&key=" + _APIKey);
                using (var webResponse = webRequest.GetResponse())
                {
                    using (var responseStream = webResponse.GetResponseStream())
                    {
                        using (System.IO.StreamReader reader = new System.IO.StreamReader(responseStream))
                        {
                            responseText = reader.ReadToEnd();
                        }
                    }
                }
                var jsonData = (new JavaScriptSerializer()).Deserialize <Dictionary <string, dynamic> >(responseText);
                if (jsonData.ContainsKey("status") == true)
                {
                    if (jsonData["status"] == "OK")
                    {
                        if (jsonData.ContainsKey("longUrl") == true)
                        {
                            return(jsonData["longUrl"]);
                        }
                    }
                }
            }
            catch (Exception)
            {
                return("");
            }
            return("");
        }
예제 #4
0
        public UploadResult ShortenURL(string url)
        {
            URLShortenerService service = UploaderFactory.URLShortenerServices[Info.TaskSettings.URLShortenerDestination];

            if (!service.CheckConfig(Program.UploadersConfig))
            {
                return(GetInvalidConfigResult(service));
            }

            URLShortener urlShortener = service.CreateShortener(Program.UploadersConfig, taskReferenceHelper);

            if (urlShortener != null)
            {
                return(urlShortener.ShortenURL(url));
            }

            return(null);
        }
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Register(UserViewModel model, string returnUrl = null)
        {
            var errors = ModelState.Values.SelectMany(v => v.Errors);

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName       = model.Email,
                    Email          = model.Email,
                    Name           = model.Name,
                    Reknown        = model.Reknown,
                    Bio            = model.Bio,
                    EmailConfirmed = false
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=532713
                    // Send an email with this link
                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Action(nameof(ConfirmEmail), "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                    URLShortenerService urlShortener = new URLShortenerService();

                    await new EmailServices().SendEmailAsync(model.Email, "Confirm your account",
                                                             urlShortener.ShortenIt(callbackUrl), EmailServices.EmailType.Register);

                    ApplicationRole mRole = null;
                    if (model.ApplicationRoleId != null)
                    {
                        mRole = await _roleManager.FindByIdAsync(model.ApplicationRoleId);
                    }
                    ApplicationRole applicationRole = mRole != null && mRole.Name.Equals("Admin") ? mRole : await _roleManager.FindByIdAsync(_roleManager.Roles.Single(r => r.Name.Equals("User")).Id);

                    if (applicationRole != null)
                    {
                        IdentityResult roleResult = await _userManager.AddToRoleAsync(user, applicationRole.Name);

                        if (roleResult.Succeeded)
                        {
                            BlobServices bs = new BlobServices();
                            if (user.ProfilePictureImage != null)
                            {
                                user.ProfilePictureImage = await bs.UploadImageToBlobStorageAsync(Convert.FromBase64String(model.ProfilePictureImage), user.Id);
                            }
                            await _userManager.UpdateAsync(user);

                            string message = "User created a new account with password.";
                            _logger.LogInformation(3, message);
                            return(Ok(Json(new { message = message, user = user })));
                        }
                    }
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(BadRequest(Json(new { messages = errors })));
        }