Jun 12, 2009

How to run a C# application as an administrator

To have your C# (or any .NET program) run as Administrator in Windows, you'll have to create a manifest for it.
Step 1:
Add a manifest file to your app.
Solution Explorer>Rightclick>Add>"New Item">"Application Manifest File"




Step2 : Manipulate the default manifest file as follows

Just change the highlighted attribute from a"asInvoked" to "requireAdministrator"



Step 3: Make your app to use the manifest
[If Not using Visual Studio]
1. Rename it to (YourEXEName).manifest. The .NET Framework when executing the file will see the Manifest and handle its contents.
2.Embed the .manifest file into you EXE. This can be done by executing the following command line:
1.mt -manifest YourProgram.exe.manifest -outputresource:YourProgram.exe
2.If your assembly is strong named, you will be unable to embed the manifest into it as it would invalidate the strong naming.
[If using Visual Studio]
1. Dont have to do anything more...
2. Just remember to run the Visual Studio as an Administrator, for future debugging.

Jun 11, 2009

BackgroundWorker Class and usage examples

BackgroundWorker Class:
Brief Defn: Executes an operation on a separate thread(MSDN).
Detailed Defn: The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

To execute a time-consuming operation in the background, create a BackgroundWorker and listen for events that report the progress of your operation and signal when your operation is finished. You can create the BackgroundWorker programmatically or you can drag it onto your form from the Components tab of the Toolbox. If you create the BackgroundWorker in the Windows Forms Designer, it will appear in the Component Tray, and its properties will be displayed in the Properties window.

To set up for a background operation, add an event handler for the DoWork event. Call your time-consuming operation in this event handler. To start the operation, call RunWorkerAsync. To receive notifications of progress updates, handle the ProgressChanged event. To receive a notification when the operation is completed, handle the RunWorkerCompleted event.

Note:
You must be careful not to manipulate any user-interface objects in your DoWork event handler. Instead, communicate to the user interface through the ProgressChanged and RunWorkerCompleted events.

BackgroundWorker events are not marshaled across AppDomain boundaries. Do not use a BackgroundWorker component to perform multithreaded operations in more than one AppDomain .

If your background operation requires a parameter, call RunWorkerAsync with your parameter. Inside the DoWork event handler, you can extract the parameter from the DoWorkEventArgs..::.Argument property.
---------------------------------------------------
Usage:

This class can be added both by code and also by using the toolbox
Usage 1: Simple invocation
////Declaration and Instantiation
BackgroundWorker Worker = new BackgroundWorker();

////Add the event handlers in the Constructor
public Form2()
{
InitializeComponent();
Worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
}

/////The Do work method is performed in a Async/multithreaded way when the
////RunWorkerAsync method of the background worker is invoked.
////N.B.: No UI Control manipulation is permitted.
void Worker_DoWork(object sender, DoWorkEventArgs e)
{
// Do not access the form's BackgroundWorker reference directly.
// Instead, use the reference provided by the sender parameter.
BackgroundWorker bw = sender as BackgroundWorker;

// Extract the argument.
int arg = (int)e.Argument;

// Start the time-consuming operation.
e.Result = TimeConsumingOperation(bw, arg);

// If the operation was canceled by the user,
// set the DoWorkEventArgs.Cancel property to true.
if (bw.CancellationPending)
{
e.Cancel = true;
}
}

////This is the method which is automatically called when the async thread has completed its work.
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
// The user canceled the operation.
MessageBox.Show("Operation was canceled");
}
else if (e.Error != null)
{
// There was an error during the operation.
string msg = String.Format("An error occurred: {0}", e.Error.Message);
MessageBox.Show(msg);
}
else
{
// The operation completed normally.
string msg = String.Format("Result = {0}", e.Result);
MessageBox.Show(msg);
}
}

=============================================================
Usage 2: Implementing BackgroundWorker with Progress Bar
http://msdn.microsoft.com/en-us/library/waw3xexc.aspx


Jun 5, 2009

Consuming and Publishing RSS feeds

1. http://www.code101.com/Code101/DisplayArticle.aspx?cid=48
2. http://www.dotnetcurry.com/ShowArticle.aspx?ID=137&AspxAutoDetectCookieSupport=1
3. http://www.c-sharpcorner.com/UploadFile/pwright/RssFeedProject12062005000807AM/RssFeedProject.aspx
4. http://aspalliance.com/919_Awesome_ASPNET_20_RSS_ToolKit_Released.1 -- (Detailed Tutorial)
5. http://blogs.msdn.com/dmitryr/archive/2006/02/21/536552.aspx

WCF certificate authentication with IIS7

1. http://romualdas.spaces.live.com/blog/cns!DCDC5E439E70339D!1006.entry
2. http://social.technet.microsoft.com/Forums/en-US/winserversecurity/thread/3bfd8974-ac8a-4c53-ade8-e8b71713f92a
3. http://forums.iis.net/
4. http://notgartner.wordpress.com/2007/09/06/using-certificate-based-authentication-and-protection-with-windows-communication-foundation-wcf/

Creating a WSE 3.0 Enabled Web Service (Tutorial)

http://dotnetslackers.com/articles/aspnet/GettingStartedCreatingWSEEnabledWebService.aspx

IIS 7 SSL Certificate Installation

1. http://pro-studio.spaces.live.com/blog/cns!7CEDD8FA2A6B9050!746.entry
2. http://www.experts-exchange.com/Software/Server_Software/Email_Servers/Exchange/Q_23860405.html
3. http://help.godaddy.com/article/4801

N.B.: If you are going to install a certificate which has a p7b extension, then rename it to .cer before u start.

Jun 2, 2009

Create Digital Signature for your App

If your application does not have a digital signature and has uiAccess=true in its manifest, it will fail with "A referral was returned from the server."

Applications that request uiAccess=true must have a valid, trusted digital signature to execute.

Also, applications by default must reside in a trusted location on the hard drive (such as windows or program files) to receive the uiAccess privilege. They will still run if they are not in one of these locations, but they will not receive the privilege. You can disable this security feature through the local security policy mmc snap-in.

If you want to create a trusted "test" certificate to sign your application with so that you can use your application on your current machine, here's how:

NOTE: These instructions assume you have visual studio installed and are using a command prompt that has all the environment variables set to find SDK utilities such as makecert and signtool. If not, you will need to find these tools on your hard drive before running them.

***

1) Open an elevated command prompt

- Click start

- Find Cmd Shell or command prompt

- Right-click, click Run As Administrator

2) Create a trusted root certificate

- Browse to the folder that you wish to contain a copy of the certificate

- In the command shell, execute the following commands:

makecert -r -pe -n "CN=Test Certificate - For Internal Use Only" -ss PrivateCertStore testcert.cer

certmgr.exe -add testcert.cer -s -r localMachine root

3) Sign your file

- In the command shell, browse to the location of your exe

- In the command shell, type:

SignTool sign /v /s PrivateCertStore /n "Test Certificate - For Internal Use Only" /t http://timestamp.verisign.com/scripts/timestamp.dll APP.exe

Where APP.exe is your application.