Wednesday 22 September 2010

The Google Way - Doing illegal things legally...

Couple of weeks back when I logged into my gmail box, there is a box popped out and said "Hey Dude! We introduced a new feature... Have you checked out? If not so just click the red link at the top right corner." The link says "New! Priority Inbox Beta". My friends buzzed already about this... Hey man, we already watched it and liked it; how about you?




I just clicked that link and google provided an youtube video they uploaded. It says "The spam filter is the best one (I don't understand why still I am getting spam mails from the unknown with the names of my address book). Even though you will be getting a lot of mails (a lot of junk in my inbox rather than in junk mail folder). Let us sort it for you. We will sort it based on the frequency of the type of mails you will be sending, the persons from whom or to whom the mails are received or sent. We sort it for you for free of cost... That means a personal assistant for no cost... (James Bond will say, let sexy Money Penny to do the things even she is costlier to hire to be a secretary.) Hurry click on the 'Try it Now' button."

But... Wait a minute... Reading my mailing pattern... Isn't it a illegal thing to read the other's mails or mailing pattern? Isn't it peeping into my privacy? The questions popped out in my mind and I just clicked "Dump your ideas Google..." (No button obviiously). I explained my friends how it don't work for me... I am getting a forwarded mail from my friend with a subject "Job Opening for freshers" and I am forwarding it to my other friends; whomever is having any siblings falling in that category and I am getting some replies from my friends with the same subject line. Now google reads this subject line and keeps it in it's database. And based on the keyword of the last mail chain, the "Priority Inbox" will sort my mail. Now I am getting a spam advertisement from some unidentified source, what google will do? Based on the keyword, it will sort the spam in my "Priority Inbox" instead of the spam mail folder.

You got it... Again coming back to the persons in touch; the people to whom I am frequently mailing and from whom I am receiving the mails from also will be read and stored. Based on this pattern also I'll receive the mails into my Priority mail box. If the mail is from a real genuine user, that is ok. Even now too I am getting mails from spammers with the names from my address book, the priority inbox will increase the chance of the spammers to use the name vigorously to put the mails in my Priority Inbox with the genuine names. In this case, my real mailbox will be filled by the spams with genuine names and keywords.

I then asked my buddies "What's your take? It is your choice to use the Priority mail box but before that just read the terms and conditions of Google. Because if you are accepting the priority mail box, you are accepting that Google can do whatever it want to do with your mail content and address book that is really a legal security breach."

So friends, What's your take? I'll definitely come with more google way of illegal things in future.

Signed



The Techno Dork

Wednesday 18 August 2010

Are we in right tone? The mistaken Singleton

Hiya... I think you might have bored about my first technical blog as the writing style was too bad. I am trying to recover from the lost presentation skill. Till then forgive me for the bad writing in the technical blogs...

When I was chit-chatting with my folks, somebody thrown a question what is the Singleton Class and the definitions were discussed and one folk came out with a word that the "Static" classes are singleton. Truly speaking the "Static" classes are not pure singleton class.

If we look close at the Static classes, the definition says it as static; for which you cannot instantiate the class. Whenever the class is defined as static, you cannot create a object. Instead you need to access the methods of the static classes by .. But for a singleton class you can create object. You can create constructors for the singleton classes but not in case of static classes.

So let us understand how we distinguish the Static from the Singleton. The Static class is defined like the following code:

Public Static Class myStaticClass
{
       public string myfirstMethod()
        {
                return "abcd";
        }

And when we are calling the method myfirstMethod in a class that needs to invoke this method we use the following syntax:

Public Class myMasterClass
{
         public static void main()
         {
              console.Write(myStaticClass.myfirstMethod());
         }

Here if we seen we didn't create an object. Instead, we are calling the method using the class directly to call the method instead of creating the object and call the method using the object.

But the definition of the Singleton class specified that you can create one and only one object for the singleton class and you need to destroy the object before creating another object for the singleton class. So you can use a "new" keyword to create an object for the singleton class against using he class name directly in the static classes. To achieve the singleton, the class uses "private" constructor against the "public" constructor for a class.

The following line of code explains the singleton class:

public sealed class mySingleton
{
    public static readonly mySingleton _instance = new mySingleton();

    ///
    /// Private constructor prevents instantiation from other classes
    ///

    private mySingleton() { }

   public static mySingleton Instance()
    {
        get
        {
            if (_instance==null)
            {
                _instance = new mySingleton();
            }
            return _instance;
        }
    }


      public string mysingletonMethod()
      {
            return "abcd";
      }

//Other Methods for this class.

}

Now in my main class when I am creating the object for this class, it will allow to create only one object. Please look at the following code:

Public Class myMasterClass
{
         public static void main()
         {
             mySingleton msObject=mySingleton.Instance();
             Console.Writeline(msObject.mysingletonMethod());
         }
}

So, this ensures that there is only one object created for the class. Folks, I think you will do it good at creating the Static Classes and the Singleton classes. No more confusions. Be a singleton hero.

Happy researching and coding!!!

Signed
The Techno Dork

Friday 13 August 2010

Upload a file to an UNC path

ASP.NET File Upload

In ASP.NET the file upload control is used to upload the files. The following code describes how the files are uploaded in ASP.NET

FileUploadTest.aspx


FileUploadTest.aspx.cs
The above code will upload the file selected through the file upload control to the folder called "uploaddir" residing in the web application path. The upload also is having a limitation. The maximum size of the file is restricted to 4 MB by default by Microsoft. This information resides in the machine.config file. The value for the “maxRequestLength” attribute of the httpRuntime configuration section. This change affects the whole computer. The section looks like this:
When there is more than one application running in the same machine, and all the application that uses the file upload this could be a problem as all the applications now will be allowed to upload the file up to 8 MB as the configuration for the machine has been changed. If this needs to be restricted, the httpRuntime configuration section should be overridden in the web.config file of the particular application for the maximum allowed file size for the application.

The following code shows how to override the httpRuntime section in the web.config section.

[Here the file size is set to 20MB]
  
The above code allows changing the upload limit to 20 MB in the web application.  The following code allows changing the upload limit to 20 MB for a specific folder in the web application.

By using the following code, we can ask the user not to upload a file of more than 20 MB.

 
So now we are familiar with the asp.net file upload as well large file upload. But in some cases, the file server that stores the files may not be the same as the server in which the web application is running. In this case how we can upload the file. For this only the UNC uploading lends a hand to upload the file in such scenario.  Now we need to know what an UNC path is, right. Here is the help about the UNC path in Microsoft environment.

What is an UNC?
 
The Microsoft Windows UNC, short for Universal Naming Convention or Uniform Naming Convention, specifies a common syntax to describe the location of a network resource, such as a shared file, directo-ry, or printer on a local area network (LAN). The UNC syntax for Windows systems has the generic form such as "\\ComputerName\SharedFolder\Resource".

The UNC Path can be assigned with the drive name in the system that is accessing the folder. For exam-ple, if the "System-A" have the shared folder called "MySharedDir", then the UNC path can be accessed from "System-B" in such a way that "\\System-A\MySharedDir". Otherwise we can create a drive letter by mapping the folder to the "System-B" in the following way:
  1. Double-click the "My Computer" icon in System-B
  2. Click on "Tools" menu and select "Map Network Drive". It opens a dialog box.
  3. In the dialog box select the "Drive" and provide the shared path (\\system-A\MySharedDir) in the "Folder" box. Select the check-box "Reconnect at Logon" to connect the folder automatically next time. Otherwise the system will disconnect the network drive whenever the System-B is switched off.
  4. Click on "Finish" button to complete the mapping. The mapped folder will open immediately in the system-B with the title "MySharedDir on System-A". We can also provide different label to the mapped path.
So we created an UNC path. This is the direct way. If both the systems (folder sharer as well the mapped system) are under the same domain/workgroup then this will work. If both the machines are under differ-ent domains then there must be a trust created between these two domains so that the machine that maps the network resource/folder will have the correct permissions to read, write or change.

Now we are ready to upload the files into a file server that is separated from the web server. But is it safety to directly give the UNC path in the given web application. It is considered to be safe to use the UNC path directly in an intranet site but it is better not to use the UNC path directly in the internet site. So now we are going to see how to upload the files to the UNC path for the internet.

1. For this first create a virtual directory in the IIS that refers the UNC path (shared folder) as the physical directory.


2. As the part of security measure, the IIS will ask for a User Name and password. Whenever we are uploading the files to the folder, the IIS uses this user name and password for accessing the folder. So give a user name and password. The user name should be plain text; not to prefix the domain name or the workgroup name to the user name (“username” instead of “domain-name\username”).



3. When you click on the next, the dialog will ask to confirm the password.


4. Now grant the permissions that you want to give to this Virtual Directory. As we are going to upl-oad files into this folder only write permission other than the default (Read and Run Scripts) is enough.


5. Now click finish to complete the virtual directory creation.


As we provided the user name and password to access the virtual directory in the above steps, the ma-chine that runs the web application as well the machine that holds the physical path of the virtual directory should have the same user name and password. To create the user name and password use the following steps.

1. Right click the “My Computer” icon and select “Manage” from the menu popping out.


2. It opens the computer management console. Expand the “Users and Groups” section in the left panel.
Right click on the “Users” and select “New User” option from the menu.


3. Provide the necessary details like username, full name and the password. Unselect the option “User must change the password at next logon” and select the option “Password never expires”.




4. Click on “Create” and if the password meets the password policy of the company or the user name does not exists, the new user will be created on the system. Otherwise the fields have to be changed accordingly and click on “Close” after the successful creation of the user.


5. By clicking the user option in the left panel, we can confirm the creation of the user name.



6. Then click on the groups and double click the “Administrators” group. Click on the “Add User” button and add the user we created and click on "OK" to give the user administrative permission.

As well follow the above six steps in the machine that have the physical folder to have the same user. For the shared folder, the access permission (Read, Write, and Modify) for the following users should have been given in the sharing permission as well in the security tab.
  1. Administrators
  2. The user name we created.
  3. Local Service
  4. Network Service
  5. ASPNET Machine Account
Now we are ready to write our code to upload the files to the path existing in a file server that is existing in the same network/different network.

For this we need to read the matabase of the IIS. In windows server 2000 or later versions, which are running IIS 5 or greater versions, you can find the metabase.xml file in the “%Syste-mRoot%\System32\Inetsrv” folder (For example “C:\Windows\System32\Inetsrv”). We can read this xml file to get the path of the virtual directory. But it is not safe as if there is any issue happens to this file, the IIS will crash. So we dump this solution to read the physical path of the virtual directory from the metabase.xml file.

So I have written a utility to detect the path from the IIS. Let’s call it as common utility and the function name is “GetPhysicalPath” and the return type is “string” and a function to get VirtualPath “GetVirtual-Path”. The utility goes like the following:


 
Here we used the metabase path to query the virtual directory:


In the above metabase path, I referred to the “localhost” because it is running from my client (windows xp) system. If the application is running on a proper web server (windows 2000/2003/2008) then provide the IP address (Primary DNS) or the server name for the application.
 
As the metabase is based on the Active Directory, we are using the DirectoryEntry class to query the IIS metabase. The “VirtualDirectoryName” parameter will have the virtual directory name we created (MyUploads) and the functions finally return the physical path. We need to attach the file name from the file upload control and upload the file.
 
Oops!! In the flow I forgot one thing to say. You need to impersonate the user we created.


Happy researching and coding!!!

Signed
The Techno Dork