SharePointWale

Hello, my name is Wale Mohammed and I am an Application/Software developer. I will use my blog as an avenue to track interesting topics ranging from A to Z...

The effectiveness of Captchas in preventing SPAM cannot be overemphasized, they are especialy useful for blogs, login forms, registration forms etc.

Google's reCaptcha is very popular and quite easy to integrate into ASP.NET applications. To get more information about Google's reCaptcha, check out http://www.google.com/recaptcha.

To use this, you register your account and application and you get a public and private key with which you perform validations. The site also has useful information on how to integrate the readily available plugin into your site in less that 10minutes.

The issue is that the plugin requires FULL trust to run which is not always available especially if your asp.net web application is hosted on a shared server. You will get errors like:

 System.Security.SecurityException: That assembly does not allow partially trusted callers.

To get around this, you can implement the control manually like this:

Notes:

If an error occurs, the google results array will contain another entry for the error reason. Also the site states that in the future, multiple array entries
might be returned.

Based on the boolean value of the function listed below, you can go ahead and authorize whatever action like creating a user, approving blog comment etc.

 

1) Place the following in the markup of your aspx page (i.e. for me this was in my content area because I was using master pages). Substitute in your public key or you will get an error from google, as best practice, you should load this from your web.config.

 

 <%--Start manual Recaptcha implementation --%>
 <script type="text/javascript">
  var RecaptchaOptions = {
   theme: 'clean'
 };
 </script>
 <script type="text/javascript"
 src="http://www.google.com/recaptcha/api/challenge?k=PUBLIC KEY HERE"> </script> <noscript> <iframe src="http://www.google.com/recaptcha/api/noscript?k=PUBLIC KEY HERE" height="300" width="500" frameborder="0"></iframe> <br> <textarea name="recaptcha_challenge_field" rows="3" cols="40"
id="recaptcha_challenge_field" runat="server"> </textarea> <input type="hidden" name="recaptcha_response_field" value="manual_challenge"
runat="server" id="recaptcha_response_field"> </noscript> <%-- End manual Recaptcha implementation --%>


2) In your code behind, add the following function:

   #region Manual captcha verification
//THIS GOES IN YOUR CODE BEHIND
 public static bool ValidateCaptchaResultManually(string privateKey, string ip, string challenge, string response) { System.Net.HttpWebRequest webRequest =
(System.Net.HttpWebRequest)WebRequest.Create("http://www.google.com/recaptcha/api/verify"); webRequest.Method = "POST"; webRequest.ContentType = "application/x-www-form-urlencoded"; string parameters =
string.Format("privatekey={0}&remoteip={1}&challenge={2}&response={3}", privateKey, ip, challenge, response); byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(parameters); webRequest.ContentLength = byteArray.Length; System.IO.Stream dataStream = webRequest.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); if (webResponse.StatusCode.Equals(HttpStatusCode.OK)) { dataStream = webResponse.GetResponseStream(); System.IO.StreamReader reader = new System.IO.StreamReader(dataStream); string[] googleResults = reader.ReadToEnd().Split("\n".ToCharArray()); webResponse.Close(); if (googleResults[0].Equals("true")) { return true; } } return false; } #endregion


When making connections to a MS SQL Server (database) in .NET applications, it is typically and best practice to store connection information which will include the server's name in a web.config and/or app.config. If for whatever reason you need to change the server your database is hosted on, you simply update those web/app.configs etc.

For a server installation like SharePoint 2007 or 2010, you need to specify a SQL Server where essentially your farm config, content databases etc. live. If for whatever reason, you need to change the database server...well you are in for a long night or weekend...

To ease this requires a little bit of planning ahead but I think it is well worth it.

Meet "SQL Server Client Network Library" aka cliconfg. On almost all Windows Operating Systems, you have the ability to set an alias to a SQL Server.

Click Start -> Run -> "cliconfg.exe" and OK. This will bring up the Config tool which looks like this:

 

 

-Click on Add and fill in the necessary information (Select TCP/IP and enter your port number if you are not on a standard port or if you are connecting to a SQL Server Instance)

 

And yes, as you noticed above, this works for creating aliases to SQL Server Named Instances also. As a matter of fact, after building a new Windows Server, one of the post steps I recommend is populating this handy tool with aliases to any SQL instances that you have, that way upgrading or moving your databases is painless (at least as far as your application layer is concerned).

One final note, if you have SQL Server Installed, you can also access and update your Server Configuration by running, guess it...yes "SQL Server Configuration Manager"

 



I am fond of the ASP.NET membership provider, it is very simple and easy to use. The default configuration/setup requires a registered user to have a unique username and an email address.

However, in some applications, it is a burden to have users remember a username which may be different from their email address. Also, when a user forgets their password, they can only retrieve it by remembering their username.

So the requirement of having their username = email is a common and sometimes necessary one.

This is easily accomplished by passing the email as the email and also passing it as the username to the provider function http://msdn.microsoft.com/en-us/library/82xx2e62.aspx  e.g.MembershipUser newUser =

OLD

Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text, EmailTextbox.Text, passwordQuestion, passwordAnswer, true, out status);

NEW

Membership.CreateUser(EmailTextbox.Text, PasswordTextbox.Text, EmailTextbox.Text, passwordQuestion, passwordAnswer, true, out status);

Or by using the HTML markup, you can simply change your username field to a "Confirm Email" field and use a ASP.NET Comparer Validator control to ensure that both values match.

 

Happy coding!



Here are some basic steps to export a web part after configuring it - so that it can be re-used on multiple other pages:

  • On the web part, click Edit -> Export
  • Save the web part to any desired location on your computer
  • Close the dialog box after saving the web part
  • Navigate to the root website of your site collection i.e. http://litwareinc
  • Click Site Actions -> Site Settings -> Modify All Site Settings
  • Under "Galleries", click on "Web Parts"
  • Click "Upload" -> "Upload Document"
  • Browse to the location you saved the exported web part (on your computer) and click OK to import it
  • *On the next screen, give the web part a descriptive title and add a description
  • *On "Group", select "Specify your own value" and enter "MyCustomWebParts"
  • Click OK

  At this point, you should be able to add that web part to any page from the web part gallery.



Easiest ways I have seen to gain access to the GAC on a Windows machine:

1) Click "Start" -> "Run" -> type in "C:\WINDOWS\ASSEMBLY\GAC_MSIL\" -> OK   (replace GAC_MSIL with whatever GAC directory you are trying to access) BTW, I read this on the web a while back, but can't remember where or I would give credit back...

2) From a folder/explorer

  • Click Tools - > Map Network Drive
  • Assign a drive letter and in folder type in "\\{MachineName}\c$\windows\assembly\" Click Finish and your GAC directories should open up!

Accessing the GAC is especially useful when you want to inspect/reflect on a DLL or take a backup copy of it, before replacing it with a newer copy (we all do backups right !?).



On a MOSS 2007 publishing site, I started getting this error whenever the page is checked out and while in edit mode I try to Click on the "edit" button on a web part. Basically you get no access to make any changes to the web part. Suprisingly, on adding a second web part (of the same type) to the same zone, you can edit the second one but still not the first. Even putting the page in maintenance mode (append "?contents=1" to URL) and deleting the web part and re-adding it did not help.

What resolved this issue for me was that there was a PublishingWebControls:RichHtmlField on the page i.e. a Page Content area that contained a div with an id and a webpartid as shown below:

id=WebPartWPQ5 webpartid="77e15fe2-f556-48bb-851d-852382652e02"

This apparently was causing a conflict whenever I clicked on the edit web part's button. So, in order to resolve the issue, all I did was

  • Copy out all the HTML from the page content area to an editor/notepad
  • Check in to share Draft (to make sure it was completely gone)
  • Check out the page and paste in the HTML WITHOUT the div's id and webpartid
  • Check in the page to share draft

And now whenever I check out the page, I can make changes to the web part without any issues. I am guessing the text in the page content area, was copied from a web part like the Content Editor web part and that introduced the issue (duplicate IDs) in the first place. Anyway, this worked for me, so Goodluck!




Feb
17

Here we go...

by Wale | Tags:

Hello and welcome to my blog. My name is Wale Mohammed and I am an Application/Software Developer. My range of interest spans any .NET technologies, Java, Open Source and Web development. My primary focus is the SharePoint Application Platform and ASP.NET. As you can guess, I am a technology enthusiast and anything that can be used or done to make life easier and solve business problems is interesing to me. I approach everyday as an opportunity to learn something new.

I will primarily use this blog avenue to chronicle my adventures as a technologist...Enjoy!



About the author

...simply a technology enthusiast!

Wale's thumbnail

Month List

Widget Twitter not found.

Root element is missing.X

Sign in