Category Archives: C#

Configure IIS Website with SSL

To secure your website you can use SSL and certificates. In this post I will explain in detail how to setup your site to use a certificate, including the details of installing your own Certification Authority. Finally I will show you some C# code to work with certificates.

Part 1 will show you how to install the Certificate Authority on a Windows 2003 machine, part 2 is about creating a new website,
part 3
shows how to request a webserver certificate,
part 4 shows you how to send the request to the Certification Authority,
part 5
shows you how the CA processes the request,
part 6
shows you how to download and install the certificate on the website, 
part 7
shows you how to create a virtual folder.
Part 8 shows you how to test ths site together with some coding examples in C#.

Share

Enable the ASP.NET page again

asp.net.logoSometimes your ASP.NET tab disappears in the IIS manager. To show this tab again follow the steps below.

Steps involved

  1. Stop the IIS Admin service (and any services that depend on it)
  2. Open C:WINDOWSsystem32inetsrvMetaBase.xml in notepad or your favorite XML Editor. DELETE the line that reads ‘Enable32BitAppOnWin64=”TRUE”‘
  3. Start -> Run -> iisreset
Share

SoapUI and WCF Service testing

Test with SoapUI: modify the generated web.config

soapUI_header_logoThe web.config that is create when you create a new WCF service application does not work with SoapUI. Retrieving the WSDL will work fine but when you execute a operation on the service the following message will appear:


<s:value>s:sender< s:value="">

A solution to this problem (at least for testing purposes) is to add a custom wsHttpBinding. Create a new WCF Service application C# project. Delete all text from the web.config and add replace it with the markup below.

Important to note is that we have a custom wsHttpBinding with the security mode set to “None”. This is necessary for SoapUI to work with this service!

Now you have to create a new SoapUI project. Point it to the ?wsdl for this services and the project is created with a default request for all operations. Open the first request and press the WS-A button (on the bottom of the request editor). Check “Enable WS-A addressing”, “Add default wsa:Action” and “Add default wsa:To”.

Now execute the request and you get a proper response!

Share

How to Retrieve image from Windows Live Album

[sourcecode language=”csharp”]
namespace WindowsFormsApplication1
{
using System;
using System.ComponentModel;
using System.Net;
using System.Windows.Forms;

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string url = http://public.blu.livefilestore.com/xyz/012.jpg";
WebClient client = new WebClient();
client.Proxy.Credentials = new NetworkCredential("uname", "upwd", "udomain");
client.DownloadFileCompleted +=
new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFile(new Uri(url), @"c:localfile.jpg");
}

void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Download completed");
}
}
}
[/sourcecode]

Share

Using MSMQ with C#

First add the MSMQ component to your windows installation. Go to Add Remove programs; Add Windows Components; Select Aplication Server; press Details and check Message Queuing

Press Ok; press Finish

Configure the Message and Queing service to restart when errors occur.

  1. Go to “Start -> Administrative tools -> Computer Management”.
  2. Expand “Services and Applications”.
  3. Sselect “Services”.
  4. Select “Message Queuing”; right click and select “Properties”.
  5. Select the “Recovery tab”.
  6. Select “Restart the Service” for first, second and subsequent failures.


Create a new C# Forms project / solution with Visual Studio 2008. Add a reference to the System.Messaging .NET dll.

Add two Forms to the project. Place in the constructor of Form1 the following code:
[sourcecode language=”csharp”]
//Q Creation

if(MessageQueue.Exists(@".Private$MyQueue"))
mq = new System.Messaging.MessageQueue(@".Private$MyQueue");
else
mq = MessageQueue.Create(@".Private$MyQueue");
Queue2 q2 = new Queue2();
q2.Show();
[/sourcecode]

Share

Executing WSS in .NET 3.5 mode

By default WSS 3.0 runs under the .NET 2.0 framework. To change this to use the .NET 3.5 framework execute the following steps:

  1. Open the web.config for your site
  2. Add the following assemblies below the tag:
    [sourcecode language=”html”]
    <add assembly="Microsoft.SharePoint, Version=12.0.0.0,
    Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
    <add assembly="System.Core, Version=3.5.0.0,
    Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    <add assembly="System.Xml.Linq, Version=3.5.0.0,
    Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    [/sourcecode]
  3. Then add the following to right before (for example) the tag
    [sourcecode language=”html”]
    <system.codedom>
    <compilers>
    <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
    type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0,
    Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <providerOption name="CompilerVersion" value="v3.5" />
    <providerOption name="WarnAsError" value="false" />
    </compiler>
    </compilers>
    </system.codedom>
    [/sourcecode]
  4. Your site now runs under the .NET Framework 3.5!
Share

Use extension methods on your ASPX page

  1. Create a Webapplication project
  2. Edit the Default.aspx to look like this:
    [sourcecode language=”html”]
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
    <%@ Import Namespace="Berend.PageExtensions" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns=http://www.w3.org/1999/xhtml>
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <h1>
    <%=this.MyPageExtensionMethod()%>
    </h1>
    </div>
    </form>
    </body>
    </html>
    [/sourcecode]

  3. Remember to add the namespace (which contains your extension method) to your aspx
  4. Add a class library to your project and code the following class:
    [sourcecode language=”csharp”]
    namespace Berend.PageExtensions
    {
    public static class Class1
    {
    public static string MyPageExtensionMethod(this Page p)
    {
    return "Extension method executed!";
    }
    }
    }
    [/sourcecode]
  5. This class Class1 contains the extension method (remember to make the class and the method static)
  6. Press F5 and your extension method gets executed.

When you want to use this technique with SharePoint 2007 you have to follow the steps in this post to run WSS under the .NET 3.5 framework!

Share

SQL Injection

In the Page_load of a ASP.NET page called inject.aspx:
[sourcecode language=”csharp”]
SqlConnection con =
new SqlConnection(@"data source=(local)TEST;user id=sa;password=sa;database=Northwind");
con.Open();
string sqlstring = String.Format("INSERT INTO Categories (CategoryName, Description, Picture) VALUES (‘naam’, ‘{0}’, null)",
Request.QueryString["desc"]);
Response.Write(sqlstring);
SqlCommand cmd = new SqlCommand(sqlstring, con);
cmd.ExecuteNonQuery();
con.Close();
[/sourcecode]
Direct your browser to “……/…../inject.aspx?test=x’,null); drop table test; –” and your table Test is gone with the (North)wind.

See also this link for more information.

Share