Thursday, August 4, 2011

Creating a SOLID Visual Studio Solution - Steven Smith

I found this article really a very important for developer. So I would like to share this with you. Hope that it may help you for better development.

The SOLID acronym describes five object-oriented design principles that, when followed, produce code that is cleaner and more maintainable. The last principle, the Dependency Inversion Principle, suggests that details depend upon abstractions. Unfortunately, typical project relationships in .NET applications can make this principle difficult to follow.


In this article, describe how one can structure a set of projects in a Visual Studio solution such that DIP can be followed, allowing for the creation of a SOLID solution......

For more details please visits: Creating a SOLID Visual Studio Solution

Thursday, July 7, 2011

"System.Security.SecurityException: Security error" error message when the virtual directory points to a remote share in ASP.NET

Very recently I have faced a problem when hosting a web application in hosting server. This application is running perfectly on local PC but security exceptions occurs when hosting to a server.

Problem
I have got the following error message:

Security Exception Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Security error.

After consult with senior guys they refer the solution which is sharing with you.

Main Cause

The System.Web namespace does not have the AllowPartiallyTrustedCallersAttribute applied to it.

Any code that is not in the My_Computer_Zone code group that does not have this attribute requires the FullTrust user right. Therefore, the remote share that holds the Web applications content requires FullTrust.

Solution

To resolve this behavior, grant the FullTrust right to the remote share:

1.On the Web server, open Administrative Tools, and then double-click Microsoft .NET Framework Configuration.
2.Expand Runtime Security Policy, expand Machine, and then expand Code Groups.
3.Right-click All_Code, and then click New.
4.Select Create a new code group. Give your code group a relevant name, such as the name of the applications share. Click Next.
5.In the Choose the condition type for this code group list, select URL.
6.In the URL box, type the path of the share in the following format:
file:////\\computername\sharename\*
7.Note Replace computername with the name of the computer that is hosting the remote share. Replace sharename with the name of the share.
8.Click Next. On the next page, select Use an existing permission set, and then select FullTrust.
9.Click Next, and then click Finish.

Restart Microsoft Internet Information Services (IIS) to restart the ASP.NET worker process.

If Microsoft .NET Framework Configuration is not displayed under Administrative Tools, you can install the .NET Framework SDK to add Microsoft .NET Framework Configuration.

Alternatively, you can run the following command to make the change:

Drive:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\caspol.exe -m -ag 1 -url "file:////\\computername\sharename\*" FullTrust -exclusive on

Monday, April 18, 2011

Web Security Tip: How to prevent Denial of Service (DoS) attack?

What is a denial-of-service (DoS) attack?
In a denial-of-service (DoS) attack, an attacker attempts to prevent legitimate users from accessing information or services. By targeting your computer and its network connection, or the computers and network of the sites you are trying to use, an attacker may be able to prevent you from accessing email, websites, online accounts (banking, etc.), or other services that rely on the affected computer.

How it works?

Let's see how it make the problem,"n" is a typical connection, the user sends a message asking the server to authenticate it. The server returns the authentication approval to the user. The user acknowledges this approval and then is allowed onto the server.

In a denial of service attack, the user sends several authentication requests to the server, filling it up. All requests have false return addresses, so the server can,t find the user when it tries to send the authentication approval. The server waits, sometimes more than a minute, before closing the connection. When it does close the connection, the attacker sends a new batch of forged requests, and the process begins again--tying up the service indefinitely.

How to Prevent?
Unfortunately, there are no effective ways to prevent being the victim of a DoS or DDoS attack, but there are steps you can take to reduce the likelihood that an attacker will use your computer to attack other computers:

1.Install a firewall, and configure it to restrict traffic coming into and leaving your computer.

2.One of the more common methods of blocking a "denial of service" attack is to set up a filter, or "sniffer," on a network before a stream of information reaches a site,s Web servers. This filter can set in firewall or pro grammatically.

The filter can look for attacks by noticing patterns or identifiers contained in the information. If a pattern comes in frequently, the filter can be instructed to block messages containing that pattern, protecting the Web servers from having their lines tied up.

3.Follow good security practices for distributing your email address.

Sunday, April 17, 2011

How to save image from direct local URL to File

In my recent project its an requirement to save images from URL to hard disk or any media. After binged I have got several solutions. Now sharing you the right solution which is work for me.

Code(C#):

protected void Page_Load(object sender, EventArgs e)
{
string url = "http://img510.imageshack.us/img510/5523/aspnet4jh.jpg";
string DestinationPath ="C:/temp";
string filename = url.Substring(url.LastIndexOf('/')+1);
byte[] bytes=GetBytesFromUrl(url);
WriteBytesToFile(DestinationPath +"/"+filename, bytes);
}

static public byte[] GetBytesFromUrl(string url)
{
byte[] b;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse();

Stream stream = myResp.GetResponseStream();
//int i;
using (BinaryReader br = new BinaryReader(stream))
{
//i = (int)(stream.Length);
b = br.ReadBytes(500000);
br.Close();
}
myResp.Close();
return b;
}

static public void WriteBytesToFile(string fileName, byte[] content)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
try
{
w.Write(content);
}
finally
{
fs.Close();
w.Close();
}
}

Hope that it may help other guys. Thanks.

Saturday, April 2, 2011

Achieve MVP award from Microsoft

Yesterday I got an email from Microsoft Community ..

"
Dear Ahsan

Congratulations on getting awarded as an MVP for contributing to the technical communities!

I am Abhishek Kant, your MVP Lead and would facilitate your interaction with Microsoft including administration of award benefits and providing latest information.............

"

Thanks to Microsoft Community to give this reward to the community contributor. It is really inspired the community member to keep up their activities.

I feel lucky to join in MVP community for the first time, hope that this is a great opportunity for me to rich my experience by sharing knowledge with other MVP.

Thanks to all for their co operation.

Tuesday, March 29, 2011

How to run 32-bit ASP.NET Applications on 64-bit Windows server

In my workstation I have used 32bit PC. There is no problem every this is working nice. But an unexpected situation arise recently when I try to deploy my application in to a 64 bit windows server. I never face this problem before.

Don't worry Technet library(Microsoft) is there. Just one line need to execute to solve my problem.

Actually I need to configure IIS to run 32-bit ASP.NET Applications on 64-bit Windows server.For this, we must configure IIS to create 32-bit worker processes. For more information about running 32-bit applications on 64-bit Windows.

IIS cannot run 32-bit and 64-bit applications concurrently on the same server.To enable IIS 6.0 to run 32-bit ASP.NET applications on 64-bit Windows.

Follow this steps:


1.Open a command prompt and navigate to the %systemdrive%\Inetpub\AdminScripts directory.

2.Type the following command:
cscript.exe adsutil.vbs set W3SVC/AppPools/Enable32BitAppOnWin64 true

3.Press ENTER.

Now your 64 bit server is ready for run 32 bit application.

Some Important information

Before you configure IIS to run 32-bit applications on 64-bit Windows, note the following:

1.IIS only supports 32-bit worker processes in Worker Process Isolation mode on 64-bit Windows.

2.On 64-bit Windows, the World Wide Web Publishing service can run 32-bit and 64-bit worker processes. Other IIS services like the IIS Admin service, the SMTP service, the NNTP service, and the FTP service run 64-bit processes only.

3.On 64-bit Windows, the World Wide Web Publishing service does not support running 32-bit and 64-bit worker processes concurrently on the same server.

After configure problem may occurs

After you configure IIS 6.0 to run 32-bit Web applications, IIS stores 32-bit DLLs and ISAPIs in the %windir%\syswow64\inetsrv directory. All other IIS files, including the MetaBase.xml file, are stored in the %windir%\system32\inetsrv directory.

File access to the System32 and sub directories are transparently redirected based on the bitness of the process making that file access (64-bit processes have full access, while 32-bit processes have access to System32 redirected to Syswow64).

If your legacy applications have specific 32-bit file access needs and you notice application failures, see if the application needs to reference the new %windir%\syswow64\inetsrv to resolve the problem.

For more information visits here.

What is HTTP Handler and HTTP Module in asp.net? Co-Relation and Differences

I have little bit confusion about HTTP Handler and HTTP Module. Then I move to community and get a huge response. After studying for a while now I get a clear idea. Now sharing with you hope that it will also help other guys.


What is HTTP handler?


An ASP.NET HTTP handler is the process (frequently referred to as the "endpoint") that runs in response to a request made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes .aspx files. When users request an .aspx file, the request is processed by the page through the page handler. You can create your own HTTP handlers that render custom output to the browser.(msdn)

HTTP handlers process the request and are generally responsible for initiating necessary business logic tied to the request. Custom handlers must implement the System.Web.IHttpHandler interface. Additionally, a handler factory can be created which will analyze a request to determine what HTTP handler is appropriate. Custom handler factories implement the System.Web.IHttpHandlerFactory interface.

ASP.net execution process(Image source:MSDN)

What is HTTP Module?

An HTTP module is an assembly that is called on every request that is made to your application. HTTP modules are called as part of the ASP.NET request pipeline and have access to life-cycle events throughout the request. HTTP modules let you examine incoming and outgoing requests and take action based on the request.(msdn)

HTTP modules are executed before and after the handler and provide a method for interacting with the request.
Custom modules must implement the System.Web.IHttpModule interface. Modules are typically synchronized with events of the System.Web.IHttpModule class (implemented within the Global.asax.cs or .vb file).

The following consists of a list of events that should be considered when implementing your module:

1.BeginRequest
2.AuthenticateRequest
3.AuthorizeRequest
4.ResolveRequestCache
5.AcquireRequestState
6.PreRequestHandlerExecute
7.PostRequestHandlerExecute
8.ReleaseRequestState
9.UpdateRequestCache
10.EndRequest
11.PreSendRequestHeaders
12.PreSendRequestContent
13.Error

Co-Relation
The co-relation between HTTP handler and HTTP module is both are an integral part of the ASP.NET application.
Every request flows through a number of HTTP modules, which cover various areas of the application (i.e. authentication and session information). After passing through each module, the request is assigned to a single HTTP handler, which determines how the system will respond to the request. Upon completion of the request handler, the response flows back through the HTTP modules to the user.


Differences


HTTP Module
1.It represents more something like an modulare peace of code that's similar to another Global.asax.
2.It represent code that is in play for all page requests.
3.These are objects which also participate the pipeline.

HTTP Handler
1.Its a handler for one request.
2.It is more like a single page.
3.These are the end point objects in ASP.NET pipeline.
4.These are essentially processes the request and produces the response

Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'

Exception: Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'...