Skip to content

Instantly share code, notes, and snippets.

@Dynyx
Dynyx / NormalizeURL.cs
Created June 4, 2012 13:37
Normalize URL
public static List<string> NormalizeUrls(List<string> urls, string rootUrl)
{
Uri uri;
if (!Uri.TryCreate(rootUrl, UriKind.Absolute, out uri)) return new List<string>();
return urls.Select(url => !Uri.IsWellFormedUriString(url, UriKind.Absolute) ? string.Concat(uri.AbsoluteUri, url) : url).Distinct().ToList();
}
@Dynyx
Dynyx / ExtractURL.cs
Created June 4, 2012 13:36
Extract URLs from string
public static List<string> ExtractUrls(string source)
{
// match.Groups["name"].Value - URL Name
// match.Groups["url"].Value - URI
const string regexPattern = @"<a.*?href=[""'](?<url>.*?)[""'].*?>(?<name>.*?)</a>";
var matches = Regex.Matches(source, regexPattern, RegexOptions.IgnoreCase);
return (from Match match in matches select match.Groups["url"].Value).ToList();
}
@Dynyx
Dynyx / DownloadImage.cs
Created June 4, 2012 13:36
Download image from URL
public Image DownloadImage(string url)
{
Image image = null;
try
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.AllowWriteStreamBuffering = true;
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201";
httpWebRequest.Referer = "http://www.google.com/";
@Dynyx
Dynyx / DownloadURL.cs
Created June 4, 2012 13:35
Download webpage from URL
public static string DownloadWebPage(string url)
{
var webRequestObject = (HttpWebRequest)WebRequest.Create(url);
webRequestObject.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201";
webRequestObject.Referer = "http://www.google.com";
try
{
var response = webRequestObject.GetResponse();
@Dynyx
Dynyx / MobileBrowserDetection.cs
Created June 4, 2012 13:34
Mobile browser detection
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MobileTest
{
/*
* Add the global filer RedirectMobileDevicesToMobileAreaAttribute to the global filter collection in
* the Application_Start() of Global.asax.cs file
@Dynyx
Dynyx / Permutation.cs
Created June 4, 2012 13:34
Permutation
using System;
using System.Collections.Generic;
using System.Linq;
namespace Permutation
{
public static class Extension
{
public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> source)
{
@Dynyx
Dynyx / BuildCookie.cs
Created June 4, 2012 13:33
Create authentication cookie
public static HttpCookie BuildCookie(int userprofileId)
{
FormsAuthentication.Initialize();
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
userprofileId.ToString(CultureInfo.InvariantCulture),
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
@Dynyx
Dynyx / Application_AuthenticateRequest.cs
Created June 4, 2012 13:33
Global.asax handle Application_AuthenticateRequest
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.User == null) return;
if (!HttpContext.Current.User.Identity.IsAuthenticated) return;
var id = HttpContext.Current.User.Identity as FormsIdentity;
if (id == null) return;
FormsAuthenticationTicket ticket = id.Ticket;
string userData = ticket.UserData;
string[] roles = userData.Split(',');
@Dynyx
Dynyx / Paginate.html
Created June 4, 2012 13:32
JSON AJAX pagination
<html>
<head>
<script type="text/javascript">
var pageSize = 10;
var currentPageIndex = 0;
var pageCount = 0;
var startItemIndex = 0;
var itemsToDisplay = 0;
@Dynyx
Dynyx / ReCaptcha.cs
Created June 4, 2012 13:31
Using ReCaptcha in an MVC application
// First, install the reCaptcha library from Nuget https://nuget.org/packages/recaptcha.
// Then, create your public and private keys at http://www.google.com/recaptcha.
// 1. Create an extension method to genereate the Recaptcha javascript
public static class Extension
{
public static MvcHtmlString GenerateCaptcha(this HtmlHelper htmlHelper)
{
var html = Recaptcha.RecaptchaControlMvc.GenerateCaptcha(htmlHelper);
return MvcHtmlString.Create(html);