Wednesday, October 20, 2010

Using jquery selectors on server side(C#)

Something that i have been looking for a long time
is to use jquery selectors on the server side.
if found a great solution at fizzler project
fizzler is .NET CSS Selector Engine and you can download it from here.
from fizzler site:
A .NET library to select items from a node tree based on a CSS selector. The default implementation is based on HTMLAgilityPack and selects from HTML documents

all you have to is to download fizzler package
and add reference to this dll files:
- Fizzler.Systems.HtmlAgilityPack.dll
- Fizzler.Systems.XmlNodeQuery.dll
- HtmlAgilityPack.dll

here you can see a useful samples:

HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
html.LoadHtml(
@"<html>
<head></head>
<body>
<div id='content'>
<a target='_blank' href='http://yosi-havia.blogspot.com' class='FirstName'>Yosi</a>
<a href='http://yosi-havia.blogspot.com' class='LastName'>Havia</a>
</div>
</body>
</html>");
HtmlNode document = html.DocumentNode;

//@@@ get all elements with content id in html(result: 1 element)
IEnumerable<HtmlNode> list1 = document.QuerySelectorAll("#content");
List<HtmlNode> lst1 = list1.ToList<HtmlNode>();

//@@@ get all elements with FirstName class name in html(result: 1 element)
IEnumerable<HtmlNode> list2 = document.QuerySelectorAll(".FirstName");
List<HtmlNode> lst2 = list2.ToList<HtmlNode>();

//@@@ get all anchor tags in html(result: 2 elements)
IEnumerable<HtmlNode> list3 = document.QuerySelectorAll("a");
List<HtmlNode> lst3 = list3.ToList<HtmlNode>();

//@@@ get all elements in html(result: 6 elements)
IEnumerable<HtmlNode> list4 = document.QuerySelectorAll("*");
List<HtmlNode> lst4 = list4.ToList<HtmlNode>();

//@@@ get element with attribue name 'target' that starts with '_bl' in html(result: 1 element)
IEnumerable<HtmlNode> list5 = document.QuerySelectorAll("a[target^='_bl']");
List<HtmlNode> lst5 = list5.ToList<HtmlNode>();

Saturday, June 19, 2010

c# window service Debugging - the best way!

Very intresting and complicated issue is how to debug c# window service,
i saw many solutions for that but no one of them made me satisfied.

the best solution for that is to use a simple VS macro, the idea is to start the service and then attach to the process in the same macro.

you can see the macro installation instructions in this post
with a few changes:

Tuesday, March 16, 2010

Create attach to process shortcut in VS 2008

Tools -> Attach to Process... -> wait for the dialog -> < select the process > -> press on attach
who has the time and the patience to follow this step every time we want to debug an internt application.
I'll show u how to attach to process in one shortcut press.
for generate the shortcut, follow the steps below:

  1. Open Visual studio

  2. Press Alt+F11 to open MyMacros

  3. Right click on MyMacros-> Add-> Add existing item and browse to this file(change the file extension to .vb)

  4. Now, we have make a shortcut key to this macro, you should go to Tools -> Options and choose Environment -> Keyboard pane

  5. To find our macro we can search for the word macro in the search text box, and select the macro we want(in my case: Macros.MyMacros.AttachingToProcess.AttachToInternetApp)

  6. Choose a shortcut keys for this macro and insert them in the 'Press shortcut keys' text box, i choose ctrl+\ (u have to press them together)

  7. Press on Assign button, and ok, that's all, we finished


Friday, February 12, 2010

Create secure form without captcha

Usually when we want to create a secure form that computer programs cannot pass we use the captcha program.
captcha has a lot of disadvantages, when the main disadvantages is that the captcha is heavy, bother, and make users to run away from our form.
in the idea that i'll show in this post the user cannot notice in any difference from unsecured form.
the steps for the very simple solution is

1. generate a random names to inputs in the form
2. save the names in the session collection at the server side
3. after the user submits the forn, take the inputs names fron rhe session
4. the the inputs values from the request collection with the key names from the session

take a look at this simple example at asp .net but it will work in every server side language:
---------------------------------------------------------------------------------------
the server side code

public partial class _Default : System.Web.UI.Page
{
public string UserNameKey;
public string PasswordKey;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["UserNameKey"] = UserNameKey = Guid.NewGuid().ToString();
Session["PasswordKey"] = PasswordKey = Guid.NewGuid().ToString();
}
}
protected void lnkSend_Click(object sender, EventArgs e)
{
if (Session["UserNameKey"] != null
&& Session["PasswordKey"] != null)
{
string UserNameValue = Request[Session["UserNameKey"].ToString()];
string PasswordValue = Request[Session["PasswordKey"].ToString()];
}
}
}


the html code

<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
User name: <input type="text" name="<%=UserNameKey%>" />
<br />
Password: <input type="password" name="<%=PasswordKey%>" />
<br />
<asp:LinkButton ID="lnkSend" runat="server"
onclick="lnkSend_Click" >Send</asp:LinkButton>
</form>
</body>
</html>