Pages

Thursday, September 16, 2010

IIS7:Create website using .NET

This can be achieved in two ways. Earlier in IIS6 and below we use System.DirectoryServices. Now the .NET 3 new .NET library Microsoft.Web.Administration allows us to manage IIS settings very easy.

FirstWay:

AppPool Creation:

Private Sub CreateAppPool(ByVal appPoolMetabasePath As String, ByVal appPoolName As String)

Try
Dim newpool As DirectoryEntry
Dim apppools As DirectoryEntry

If appPoolMetabasePath.EndsWith("/W3SVC/AppPools") Then
apppools = Me.GetIISDirectoryEntry(appPoolMetabasePath)
newpool = apppools.Invoke("Create", "IIsApplicationPool", appPoolName)
newpool.CommitChanges()
Else
Throw New Exception("Failed in CreateAppPool(): Application pools can only be created in the */W3SVC/AppPools node.")
End If

Catch ex As Exception
Throw ex
End Try

End Sub

Function To Fetch Next SiteID:

Private Function GetNextSiteId(ByVal objService As DirectoryEntry) As String
Dim strNewSiteId As String = String.Empty
Dim siteIDNum As Integer = 1
For Each e As DirectoryEntry In objService.Children
If e.SchemaClassName = "IIsWebServer" Then
Dim ID As Integer = Convert.ToInt32(e.Name)
If ID >= siteIDNum Then
siteIDNum = ID + 1
End If
End If
Next
strNewSiteId = siteIDNum.ToString()
Return strNewSiteId
End Function

Craete Site and Assign To the AppPool:

Public Sub CreateSite(ByVal localPath As String, ByVal domainName As String, _
ByVal siteName As String, ByVal appPoolName As String)

Dim iisRootDirectory As DirectoryEntry
Dim sites As DirectoryEntries
Dim className As String
Dim websiteIdentificationName As String
Dim iisUnderNT As Boolean = False
Dim newSiteID As String
Dim iisAdminUserName As String
Dim iisAdminPassword As String

Try
iisAdminUserName = Utility.GetAppSettingsValue(Of String)(Utility.AppSettingsKey.IISAdminName)
iisAdminPassword = Utility.GetAppSettingsValue(Of String)(Utility.AppSettingsKey.IISAdminPwd)

' Impersonate the User with Admin User. ImpersonationManager is separete class.
Using impersonationManager As New ImpersonationManager(iisAdminUserName, iisAdminPassword, domainName)

If impersonationManager.impersonateValidUser() Then

iisRootDirectory = Me.GetIISDirectoryEntry("IIS://Localhost/W3SVC")
sites = iisRootDirectory.Children
className = iisRootDirectory.SchemaClassName.ToString()

'Determine version of IIS
Using iisRootSchema As DirectoryEntry = Me.GetIISDirectoryEntry("IIS://Localhost/Schema/AppIsolated")
If iisRootSchema.Properties.Item("Syntax").Value.ToString.ToUpper = "BOOLEAN" Then
iisUnderNT = True
End If
End Using

'SiteName for Ex: yahoo,google
websiteIdentificationName = siteName

If className.EndsWith("Service") Then
'1. Get New Site for the site to be created on IIS
newSiteID = Me.GetNextSiteId(iisRootDirectory)

'2. Create a new website and add to IISRoot and Autostart website
Dim newSite As DirectoryEntry = sites.Add(newSiteID, (className.Replace("Service", "Server")))

'3. Autostart Website
newSite.Invoke("Put", "ServerAutoStart", 1)
newSite.CommitChanges()

'4. Configure Host header information for subdomains configuration
Dim hostHeaders As Object() = {":80:" & websiteIdentificationName, ":80:www." & websiteIdentificationName}
newSite.Invoke("Put", "ServerBindings", hostHeaders)
newSite.CommitChanges()

'5. Set Authorisation flags
newSite.Invoke("Put", "AuthFlags", "1") '1 = AuthAnonymous, 2 = BasicAuth, 4 = AuthNTLM - integrated windows auth,16 = AuthMDS, 64 = AuthPassport
newSite.CommitChanges()

newSite.Invoke("SetInfo")
newSite.CommitChanges()

'6. Set .Net 2.0 Framework Version
Dim scriptMapVals As PropertyValueCollection = newSite.Properties("ScriptMaps")
Dim objScriptMaps As New ArrayList()
For Each scriptMapVal As String In scriptMapVals
If scriptMapVal.IndexOf("Framework") > 0 Then
Dim version As String = scriptMapVal.Substring(scriptMapVal.IndexOf("Framework") + 10, 9)
Dim strNewValue As String = scriptMapVal.Replace(version, "v2.0.50727")
' UGLY .. but framework version wasnt returning this value

objScriptMaps.Add(strNewValue)
Else
objScriptMaps.Add(scriptMapVal)

End If
Next
newSite.Properties("ScriptMaps").Value = objScriptMaps.ToArray()
newSite.CommitChanges()

'7. Set website name and description
Me.SetMetabaseSingleProperty(newSite, "ServerComment", websiteIdentificationName)

'8. Set Application Name
Me.SetMetabaseSingleProperty(newSite, "AppFriendlyName", websiteIdentificationName)

'9. Set Applcation Pool
Dim poolRoot As New DirectoryEntry("IIS://localhost/W3SVC/AppPools")
Dim pool As DirectoryEntry = poolRoot.Children.Add(appPoolName, "IIsApplicationPool")
'*****************************************************************
'Important: To set App Pool to run in Integrated Mode in IIS 7.0
'*****************************************************************
pool.InvokeSet("ManagedPipelineMode", New Object() {0})
pool.CommitChanges()

'10. Set Scripts Execute Permissions
Me.SetMetabaseSingleProperty(newSite, "AccessExecute", True)

'11. Set Default documents
Me.SetMetabaseSingleProperty(newSite, "DefaultDoc", "default.aspx, default.htm, index.htm, index.html")

Using createdSite As DirectoryEntry = newSite.Children.Add("Root", "IIsWebVirtualDir")

'12. Set Source Path
Me.SetMetabaseSingleProperty(createdSite, "Path", localPath)

'13. Create the webapplication on website root
If iisUnderNT Then
createdSite.Invoke("AppCreate", False)
createdSite.CommitChanges()
Else
createdSite.Invoke("AppCreate", 1)
createdSite.CommitChanges()
End If

'14. Assign Website Root to AppPool
Me.AssignWebsiteToAppPool(createdSite, IISDirectoryType.VirtualDirectory, appPoolName)

End Using

newSite.Invoke("SetInfo")
newSite.CommitChanges()
newSite.Close()
Else
Throw New Exception("A site can only be created in a node.")
End If

Else
Throw New Exception(String.Format("User Impersonation Failed: unable to impersonate user <{0}> for creating website", iisAdminUserName))
End If

End Using

Catch ex As Exception
ExceptionManager.Publish("Failed in CreateSite() with following exception: ", ex)
End Try

Exit Sub
End Sub

Thats it with System.DirectoryServices

SecondWay:
This is newer version and most simpler one compared to the Firstway. Add referecnce to the Microsoft.Web.Administration dll from DotNet components.


Tuesday, July 6, 2010

Huge File Uploads in ASP.NET

After a month and half, bosome time to keep this going.

There was an issue happening repeatedly, when we were trying to upload an image file with size 5MB through an File Upload Control in ASP.NET application.

Uploading a large file with File Upload Control gets tricky. The maximum default size is limited to 4MB by .NET framework, since some attackers may find this easier to upload large files which inturn consume huge server resources.

Increasing the Maximum Upload Size

The 4MB default is set in machine.config.

<system.web>
<httpRuntime executionTimeout="110" maxRequestLength="20000" />
</system.web>

But you can override it in you web.config. For example, to expand the upload limit to 20MB, you'd do this:

<system.web>
<httpRuntime executionTimeout="240" maxRequestLength="20480" />
</system.web>

Hope this solution might help to solve your problems. Please pass in your valuable feedbacks or doubts here.

Monday, May 3, 2010

Select Multiple Table Columns with JOINS

In one of our project(a kind of ETL project), there was a requirement to map the a single table columns with multiple table column. I searched in many forums and finally from MSDN learned a new implementation using Joins.

Here we go:

These are the three tables and their columns.

MasterTable:
ID
Name
Value
Area
State
County
CreatedDate
ModifiedDate

State:
ID
StateCode
State
CreatedDate
ModifiedDate

Area:
ID
AreaCode
Area
CreatedDate
ModifiedDate

The Area and State tables are like KeyValue pair tables, in which the AreaCode and StateCode represents the integer value for corresponding string values. The requirement is to create a View for MasterTable, in which the Area should be matched with AreaCode, and State to be matched with StateCode.

Code Snippet:

CREATE VIEW [dbo].[vMaster]
AS
SELECT m.ID,
m.Name,
m.Value,
a.AreaCode,
s.StateCode,
m.County,
m.CreatedDate,
m.ModifiedDate

FROM MasterTable m LEFT JOIN Area a
ON a.Area = m.Area LEFT JOIN State s
ON s.State = m.State

I think this might be useful. Don't feel shy to drop in your comments.

Happy coding. Have a good day.

Thursday, March 25, 2010

Copy Generic Collection List in DotNet

After a very long time I am learning something new. It doesn't mean that I was not experimenting new things, but I haven't learned something new in DotNet Programming for past 2 months. Quite a big time!!!!

Anyway Coming to the Collection List Addition,

I would like to explain you with the following collection object.

EG:
Product - Object which has properties (ProductID, ProductName, ProductDescription, DateSold, DaysOnMarket and e.t.c)

SoldProductList - Generic collection object of type Product
PendingProductList - Generic collection object of type Product
ActiveProductList - Generic collection object of type Product
TotalProductList - Generic collection object of type Product

The business logic is to add all the three SOLD, PENDING and ACTIVE list collections to a single collection TOTALPRODUCT list object.

Initially I had my code to loop through each items of the 3 Collection List and add it to the TotalProductList. I was very much sure there should be some other efficient way to make it. As I googled, I found a solution for it. I implemented the following and this helped me in drastically reducing my code and increased the performance of the application flow.

Code:
TotalProductList.getRange(ActiveProductList.ToArray());
TotalProductList.getRange(PendingProductList.ToArray());
TotalProductList.getRange(SoldProductList.ToArray());

Interesting Right!!! Well to fundamentally explain this, DotNet framework has as inbuilt method (getRange) for each collection to copy Array of elements of SAME TYPE. Here we are converting the ActiveProductList generic collection to an Array ( here the Array will be created of same type Product ) and then appending to TotalProductList collection which is also of same Product type.

I hope this helps you do better coding. Please let me know your thoughts and comments on this post.

Happy coding. Have a great time.

Tuesday, January 12, 2010

Importing data to SQL Server from Excel file

After a very long gap of a month and half, I had some space for me to write some technical blog.

This is all about exporting the Excel sheet to SQL Data source. You will need this in many instances to manipulate datas present in the sheet. These were the steps to be followed.

Step 1:

You have to check some basic configurations in your Surface Area Configuration of the SQL Server.

Please refer the following link to do these configurations:
http://www.kodyaz.com/articles/enable-Ad-Hoc-Distributed-Queries.aspx

Step 2:

Install Microsoft.ACE.OLEDB.12.0 component from this following link:
http://www.microsoft.com/downloads/details.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=en


Step 3:


The DB script for importing datas from .xlsx(Excel) file to SQL Server 2005 is mentioned below.


Simple Importing:

SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\TestExcel.xlsx', 'SELECT * FROM [Sheet1$]');


Importing Data to Table:

SELECT * INTO [dbo].[ExcelTempTable] FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\TestExcel.xlsx', 'SELECT * FROM [Sheet1$]');


Note: This doesn't mean that the [ExcelTempTable] should be predefined. Its a table created dynamic with the columns from Excel sheet.


This would suffice for exporting data from .xlsx file to SQL Server. Happy coding.









Tuesday, December 8, 2009

Application Level Error Handling in .NET

There were many chances for the occurrence of error in a .NET web application. We can easily fetch those errors and handle the users in a friendly way with the Global.asax file.

The event Application_Error in the Global.asax file deals with this. You can add this Global.asax file in the root of your web application. Inside that place the following code.

void Application_Error(object sender, EventArgs e)
{
Server.Transfer("~/CustomErrors/ErrorHandlingPage.aspx?error=" + Server.GetLastError().Message);
}

This code will fetch the last error occurred in the application and redirects to the ErrorHandlingPage.aspx. Here you can get the error message from the query string 'error'. This is how it works.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String ErrorMsg;
ErrorMsg = Request.QueryString["error"].ToString();
LitError.Text = "Sorry for the inconvenience caused. We regret the situation. Please try after some time. The following exception occured - " + ErrorMsg;
//The below line is for publishing the error to the Exception Log - Specific to the application
ExceptionManager.Publish(ErrorMsg);
}
}

Its upto you to decide, whether to show the message to the user or make in entry in the exception log and notify user with custom error message. Thats it.
Happy coding. Have a good day.

Monday, October 19, 2009

AJAX implementation using POST method in .NET

Here is where the real Ajax is implementation happens. In fact the .Net framework itself supports the partial page posting using Update Panel and etc. But what actually happens here is, the whole contents of the page goes to the server and comes back. Which is again a similar to a page postback. The only difference is the page events doesn't gets loaded again since its not a post back, but all the contents of the page goes to the server and then returns back.

The real implementation of the AJAX happens only with the JavaScript functions. Here you can really send the partial contents to the server and get back the desired results. There were two ways for the AJAX form posting.
1. GET method and
2. POST method.

Most people tend to go for the GET method since its easier to implement.(Particularly beginners). As you grow you have to be able to identify the instance where you are going to use(GET or POST method). In a simple way,

GET - should be used when a minimum amount of data is going to be passed to the server and collect all the information in a single AJAX call.

POST - when the request is going to be updated frequently and also the server data is going to be changed with the AJAX call then you have to go for POST. Also POST is secured compared to GET method since it doesn't expose the parameters(Query String values).

Here is the example where the POST implementation for simple Login.
EG:

Java script Function


function ajaxPOSTFunction(email,password)
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("Your browser does not support XMLHTTP!");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
if (xmlhttp.status == 200)
{
if(xmlhttp.responseText.match("success"))
{
window.location = '<%= ResolveClientUrl("../profile.aspx") %>';
}
else
{
document.getElementById("validateTips").innerHTML = xmlhttp.responseText;
}
}
}
}

//GET METHOD IMPLEMENTATION - Same implementation using GET - commented
//xmlhttp.open("GET","login.aspx?email=" + email + "&password=" + password,true);
//xmlhttp.send(null);


//POST METHOD IMPLEMENTATION
var params = "email="+email+"&password="+password;
xmlhttp.open("POST",'<%= ResolveClientUrl("../login.aspx") %>',true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
}

And here is the code for the login.aspx page - PageLoad() and Private method

Login.aspx

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim email As String = Request.Form("email")
Dim password As String = Request.Form("password")
AuthenticateUser(email, password)
Catch ex As Exception
Response.Write(ex.ToString())
End Try

End Sub

Public Sub AuthenticateUser(ByVal userName As String, ByVal password As String)
Dim ErrorBuilder As New StringBuilder
Dim responseStatus As Integer
Dim responseText As String = String.Empty

responseStatus = Me.Controller.AuthenticateUser(userName, password)
If responseStatus = Constants.ServiceResponseStatus.Success Then
responseText = "success"
End If

Select Case responseStatus

Case Constants.ServiceResponseStatus.Success

Response.Write(responseText)
Response.End()

Case Constants.ServiceResponseStatus.Failure
Response.Write("failure")
Response.End()

Case Constants.ServiceResponseStatus.Exception
Response.Write("There has been an internal error. Please try to login again!")
Response.End()

Case Else
Response.Write("The username or the password you provided was not a valid. Please login with valid username and password.")
Response.End()

End Select

End Sub