Sunday, December 15, 2013

Adding or Removing HTTPS Binding

If you are trying to delete a website that is using a wildcard ssl certificate, Or you are trying to remove ssl binding from a website via IIS, you may come across
following type of warning messages.

The certificate associated with this binding is also assigned to another site's binding. Deleting this binding will cause the HTTPS binding of the other site to be unusable. Do you still want to continue?

OR

The certificate associated with this binding is also assigned to another site's binding.  Editing this binding will cause the HTTPS binding of the other site to be unusable. Do you still want to continue?

Solution is to use command prompt to add or remove ssl binding. (Following command was tested on IIS7)

Adding Https Binding

c:\windows\System32\inetsrv>appcmd set site /site.name:"YOURWEBSITE.COM" /+bindings.[protocol='https',bindingInformation='*:443:YOURWEBSITE.COM']

Removing Https Binding

c:\windows\System32\inetsrv>appcmd set site /site.name:"YOURWEBSITE.COM" /-bindings.[protocol='https',bindingInformation='*:443:YOURWEBSITE.COM']


To view all websites/bindings type the following command

appcmd list site

Monday, November 19, 2012

Static Members VS Application Object in Web Application


The reason to provide Application object in ASP.NET was primarily backward compatibility with the Classic ASP so that it is easier to migrate Classic ASP application to ASP.NET.

It is recommended to keep the application wide information in static variables instead of Application Object,  because static variables are faster than you can access an item in the Application dictionary.

More Information : http://support.microsoft.com/default.aspx?scid=kb;en-us;Q312607

Friday, January 28, 2011

Linq to SQL Mapping by decorating classes with special attributes


With LINQ to SQL, you can set up mappings between C# classes and an implied database schema either
by decorating the classes with special attributes or by writing an XML.

Following is a c# 4.0 code that describes first approach i.e. Linq to Sql mapping by decorating classes with special attributes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.Mapping;
using System.Data.Linq;

[Table(Name="Jobs")] public class Job
{
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
internal int JobID { get; set; }
[Column] public string JobTitle { get; set; }
[Column] public long Salary { get; set; }
}

[Table(Name="Applications")] public class Application
{
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
internal int ApplicationID { get; set; }
[Column] public int JobID  { get; set; }
[Column] public DateTime DateApplied { get; set; }
}

String ConStr=”"Data Source=localhost;Initial Catalog=JobsDB;Integrated Security=SSPI;"
DataContext dc = new DataContext(connectionString); // Get a live DataContext
dc.GetTable<Job>(); // Tells dc it's responsible for persisting the class Job
dc.GetTable<Application>(); // Tells dc it's responsible for persisting the class Application
dc.CreateDatabase(); // Causes dc to issue CREATE TABLE commands for each class

Above code will create JobsDB along with tables Jobs and Applications.


Tuesday, December 14, 2010

SilverLight - Fill a ComboBox


Just doing some test

Binding a ComboBox at runtime.
private void button1_Click(object sender, RoutedEventArgs e)
{
List<Car> carList = new List<Car>();
carList.Add(new Car() { Name = "Ferrari", Price = 150000 });
carList.Add(new Car() { Name = "Honda", Price = 12500 });
carList.Add(new Car() { Name = "Toyota", Price = 11500 });
comboBox1.ItemsSource = carList;
comboBox1.DisplayMemberPath = "Name";
comboBox1.SelectedIndex = 2;
comboBox1.SelectionChanged += new SelectionChangedEventHandler(comboBox1_SelectionChanged);
}
void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Car selCar =(Car) comboBox1.SelectedItem;
MessageBox.Show(selCar.Name);
}
}
public class Car
{
public string Name { get; set; }
public int Price { get; set; }
}

Binding a ComboBox at design time;
<ComboBox Height="23" HorizontalAlignment="Left" Margin="225,184,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" ItemsSource="{Binding}" >
            <ComboBoxItem Content="Value-1"></ComboBoxItem>
            <ComboBoxItem Content="Value-2"></ComboBoxItem>
            <ComboBoxItem Content="Value-3"></ComboBoxItem>
            <ComboBoxItem Content="Value-4"></ComboBoxItem>
            <ComboBoxItem Content="Value-5"></ComboBoxItem>
</ComboBox>

Monday, December 13, 2010

TripleDES Encryption using MD5 Hashing

Example in VB.Net
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
 
Public NotInheritable Class Utility

 Private Sub New()
End Sub


Private
Shared Function CreateDES(ByVal key As String) As TripleDESDim md5 As MD5 = New MD5CryptoServiceProvider()
Dim des As TripleDES = New TripleDESCryptoServiceProvider()
des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key))
des.IV = New Byte(des.BlockSize \ 8 - 1) {}
Return des
End Function


Public
Shared Function Encryption(ByVal PlainText As String, ByVal key As String) As String
Dim
des As TripleDES = CreateDES(key)
Dim ct As ICryptoTransform = des.CreateEncryptor()
Dim input As Byte() = Encoding.Unicode.GetBytes(PlainText)
Return Convert.ToBase64String(ct.TransformFinalBlock(input, 0, input.Length))
End Function


Public
Shared Function Decryption(ByVal CypherText As String, ByVal key As String) As String
Dim
b As Byte() = Convert.FromBase64String(CypherText)
Dim des As TripleDES = CreateDES(key)
Dim ct As ICryptoTransform = des.CreateDecryptor()
Dim output As Byte() = ct.TransformFinalBlock(b, 0, b.Length)
Return Encoding.Unicode.GetString(output)
End Function


End
Class

 
Example:
Dim strEncrypted As String = Utility.Encryption("Hello", "TESTKEY3327ABC09876433")
Dim strDecrypted As String =
Utility.Decryption(strEncrypted,TESTKEY3327ABC09876433")MessageBox.Show(strDecrypted)

Wednesday, December 8, 2010

Code Contracts in .NET 4

Reference : http://visualstudiomagazine.com/articles/2010/06/23/code-contracts.aspx
http://www.codeproject.com/KB/cs/CodeContracts.aspx

One of the key features of the .NET Framework is its enforcement of strong typing. But sometimes, simply enforcing that an integer is passed into a method isn't enough. We have to write additional code to make sure the integer is in a particular range or some other requirement. With Code Contracts, we get a language-neutral way to express such additional requirements in the form of preconditions, postconditions and invariants.
What is Code Contracts
Code Contracts is a subset of a larger Microsoft Research Project called Spec# (pronounced "Spec Sharp"). You can read more about this project here. Spec# is a formal language for defining API contracts. A subset of these contracts -- preconditions, postconditions and invariants -- has made its way into Visual Studio 2010 in the form of Code Contracts.
Before being able to utilize Code Contracts, you'll need to download and install it from http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx. Why the need for the download? Code Contracts has support for both Visual Studio 2008 as well as 2010. Since it's not a VS 2010-specific product, a separate download is required.
Example :
using System.Diagnostics.Contracts;

public class TestContract
    {
        int m_n1;
        int m_n2;
        public TestContract(int x,int y)
        {
            Contract.Requires(x > 10, "x should be > 10");
            m_n1 = x;
            m_n2 = y;
        }       
    }


Now try to create an object of TestContract
                TestContract obj = new TestContract(3, 20);

This will throw exception as “x should be > 10”

Parallel Programming with dot net

http://msdn.microsoft.com/en-us/library/ff963553.aspx

Example.
using System.Threading.Tasks;
int n=5;
Parallel.For(0, n, i => {
MessageBox.Show(i.ToString());
});