Friday, May 27, 2005

.Net Remote Script Request

Have you ever wanted an easy way to call a server side method from javascript? If so this class may be just what you need.

I have created a simple and easy to use method for accomplishing this task. All you have to do is have your webform inherit from the remote script page, make your call using a simple javascript object (will be included automatically), and when you execute the request in javascript the OnRemoteScriptRequest event will be raised in your server side code, you then process the request and write the result out on the response stream. The result will be returned by the javascripts execute method.

This .Net class library uses a combination of C# code and javascript to accomplish the task in an easy to use manner. The call uses Microsoft.XMLHTTP, Msxml2.XMLHTTP or XMLHttpRequest objects (depending on the browser) to do the underlying call back to the server. Because of this it will work in all recent IE and Mozilla based (Firefox, Netscape, Mozilla, etc.) browsers.

Try It Here!

Here it is step by step
  1. Include a reference to RemoteScript.dll
  2. Have your webform inherit from the remote script page:
      public class WebForm1 : RemoteScript.Pages.RemoteScriptPage
  3. Add the event handler in the InitializeComponent method
      this.OnRemoteScriptRequest += new RemoteScript.Pages.RemoteScriptEventHandler(RemoteScriptTest_OnRemoteScriptRequest);
  4. Add the event handler method
      private void RemoteScriptTest_OnRemoteScriptRequest(object sender, ...
  5. After you add your code to the event handler you will be able to call it from javascript using the RemoteScriptRequest javascript object, Example:

      objRS = new RemoteScriptRequest();
      strReturnValue = objRS.Execute();


Donload the source code and a sample webform here

You can view the documentation here: html - chm






Thursday, May 26, 2005

C# / .Net Gmail Tools

I have created a gmail class library that provides some tools for getting gmail atom feeds and for sending emails through gmail using the .Net framework. These tools are not very complicated, but they are simple to use and should save a user some time if they want to integrate an application with gmail.

GmailMessage
Sending email using the System.Web.Mail namespace is very simple, but there is not a straight forward way of sending email using a secure connection or on different ports, which is required by gmail. Because of these drawbacks I created the GmailMessage object that inherits from the MailMessage object, all you have to do is set up the message object and call its send message.

I also added a couple of static methods that allow you to send a email through your gmail account in as little as one line of code. Below are some examples of it's usage.
//Send a message with one line of code RC.Gmail.GmailMessage.SendFromGmail("username", "password", "toAddress@gmail.com", "subject", "message body"); //Send a message with one line of code with a MailMessage object RC.Gmail.GmailMessage.SendMailMessageFromGmail("username", "password", mailMessageObject); //User the GmailMessage object to create and send your message RC.Gmail.GmailMessage gmailMsg = new RC.Gmail.GmailMessage("username", "password"); gmailMsg.To = "RCcode@gmail.com"; gmailMsg.From = "fromAddress@gmail.com"; gmailMsg.Subject = "C# Test Message"; gmailMsg.Body = "Test body"; MailAttachment attachment = new MailAttachment(@"c:\testfile.txt"); gmailMsg.Attachments.Add(attachment); gmailMsg.Send();


GmailAtomFeed
The GmailAtomFeed class provides a simple object layer for programmatic access to gmails atom feed. In just a couple lines of code the feed will be retreived from gmail and parsed. After that the entries can be accessed through an object layer AtomFeedEntryCollection, plus access to the raw feed and the feeds XmlDocument is also available.

Below are some examples of it's usage. // Create the object and get the feed RC.Gmail.GmailAtomFeed gmailFeed = new RC.Gmail.GmailAtomFeed("username", "password"); gmailFeed.GetFeed(); // Access the feeds XmlDocument XmlDocument myXml = gmailFeed.FeedXml // Access the raw feed as a string string feedString = gmailFeed.RawFeed // Access the feed through the object string feedTitle = gmailFeed.Title; string feedTagline = gmailFeed.Message; DateTime feedModified = gmailFeed.Modified; //Get the entries for(int i = 0; i < gmailFeed.FeedEntries.Count; i++) { entryAuthorName = gmailFeed.FeedEntries[i].FromName; entryAuthorEmail = gmailFeed.FeedEntries[i].FromEmail; entryTitle = gmailFeed.FeedEntries[i].Subject; entrySummary = gmailFeed.FeedEntries[i].Summary; entryIssuedDate = gmailFeed.FeedEntries[i].Received; entryId = gmailFeed.FeedEntries[i].Id; }
That's about it short and sweet, I hope this saves you time integrating with gmail from your applications.

Download the source code here: GmailHelper.zip

You can view the documentation here: html - chm




Wednesday, May 25, 2005

Simple C# RSA Encryption Class

Well, you can figure this out yourself, use it if you like, tell me how much it sucks, what ever you want.

What it does is takes data and encrypts it using DES encryption and then encrypts the DES Keys using RSA and stores everything in an XML string to be saved for later decryption. There are also classes to hold the encrypted data and to create and save the RSA keys. This seems to be a pretty good solution for encrypting data or files.

I wrote this code and am now posting it because I did not find any simple snippets on the web for encrypting data in C#. Everything seemed too complicated or was not robust enough. I am sure that many people may have problems with this code or the lack of explaination in the code, but oh well... Its here for those who want to look it over. Remember, use at your own risk!

Code:





Tuesday, May 24, 2005

C# LateBound COM

I made a very simple wrapper for calling com objects in c# and I thought you may find it useful. Currently there are only a couple methods, but they are the most useful items.

Sample usage:
//Create object object myComObject = ComUtil.CreateObject("SomeObject.SomeClass");

//Call Method
object[] parameters = new Object[]{paramValue1, paramValue2, paramValue3, paramValue4};
ComUtil.CallMethod(myComObject, "MethodName", parameters);

//Get a property
Int16 returnValue = (Int16)ComUtil.GetProperty(myComObject, "PropertyName");

//Set a property
ComUtil.SetProperty(myComObject, "PropertyName", value);

//Call a method with a ByRef parameter (recordset)
ADODB.Recordset rs = null;

ParameterModifier byRefParamMod = new ParameterModifier(1);
byRefParamMod[0] = true;

ParameterModifier[] pmArray = {new ParameterModifier(1), byRefParamMod, new ParameterModifier(1),new ParameterModifier(1)};

object[] parameters = new Object[]{paramValue1, rs};
ComUtil.CallMethod(myComObject, "MethodName", parameters, pmArray);



//Call a method with a return value
ADODB.Recordset rs = (ADODB.Recordset)ComUtil.CallMethod(myComObject, "MethodName", null);

Here is the class:

Monday, May 23, 2005

QueryString Encryptor HTTPModule

I wrote an HttpModule that Encrypts querystrings.  This module is seamless to the web developer. You can create html that looks like this: myPage.aspx?id=1&customer=2 and the source on the client will automatically be converted to this: myPage.aspx?eqs=KS%2bthrckechBKT%2bZ8IB44Bz3qvW3853f. Then to access the value in the code behind page you would use QueryString["id"] or QueryString["customer"].

To install it add QSHttpModules.dll to your bin directory and then add the following to your web.config (any where in <system.web>):

<httpModules>
   <
add type="QSHttpModules.QueryStringEncryptor, QSHttpModules" name="QueryStringEncryptor" />

</httpModules>


The module does this by finding the links and replacing them with the encrypted version as the page is sent out to the client. Then on every request the module looks for an encrypted querystring, if found it decrypts it and rewrites the url.

The code is listed below, but you can also download it from my message board at: http://csharpboard.com/ShowPost.aspx?PostID=44. This module of course takes some overhead to process and it is not recommended to be used as a full security feature. Rights checking should always be in place. But, if a little cpu time is worth hiding the contents of your querystring, this may be for you.

As always, use at your own risk.


Code:

Sunday, May 22, 2005

Javascript Color Picker

I wrote a javascript color picker. I know that there are plenty out there, but I just did not like the way they looked or worked very much. So, I decided to add one more color picker to the world. The down side to this color picker is that it uses external files, but to me it is worth it. It has a photoshop feel that I like.

You will find a working sample below and you can download the code and a test page here. As always you will have to figure the code out yourself and you must use at your own risk.

Sample:





Saturday, May 21, 2005

C# - Loading and creating an object instance from a file

This sample is pretty simple. It is basically loading a latebound object. I am not sure how often most people will have to do this, but I did so I will share it.

This method does not do that much. It will load an assembly from the provided file path and then attempt to create an instance of the type specified. If an empty string is provided for the type, it will create an instance of the first type in the assembly. If there is an error then null is returned.

Use: myType myObj = (myType)CreateLateBoundObjectFromFile(@"c:\myFilePath.dll", "myType");

Code:

Friday, May 20, 2005

.NET Zip Library #ziplib (SharpZipLib)

.NET Zip Library #ziplib (SharpZipLib)

I have used the SharpZipLib for some time now and it is great. It's easy to use and does everything that you may need; Zip, GZip, Tar, etc.

Lately, I have seen quite a few third party .Net zip components for sale and its hard for me to believe that someone would buy a component when such a good one exists for free. It must be that not enough people know about this free library. Well, here it is: http://icsharpcode.net/OpenSource/SharpZipLib/Default.aspx