Thursday, September 3, 2015

Block or Allow IP in Azure Cloud Service


To block or allow access of IP for your azure cloud service you need to write the following code in ServiceConfiguration.cscfg under </Role> tag:

<NetworkConfiguration>
    <AccessControls>
      <AccessControl name="test">
        <Rule action="permit" description="test" order="100" remoteSubnet="192.72.174.38/32" />
        <!--<Rule action="deny" description="test" order="200" remoteSubnet="0.0.0.0/0" />-->        
      </AccessControl>
    </AccessControls>
    <EndpointAcls>
      <EndpointAcl role="MyApp" endPoint="Endpoint1" accessControl="test"/>
    </EndpointAcls>
    <!--<AddressAssignments>
      <ReservedIPs>
        <ReservedIP name="YourStaticIP"/>
      </ReservedIPs>
    </AddressAssignments>-->
  </NetworkConfiguration>

Here I am allowing a specific IP and denying all IPs this is for my staging site.

Here "MyApp" - is my web role name
and "Endpoint1" - is my endpoint name as in csdef file.
In remoteSubnet="0.0.0.0/0" / section /0 is CIDR notation.

Tuesday, August 11, 2015

Client Side Session Timeout Management

Requirement:
After login if the user is idle for long time then show him a modal window with a count down timer and 2 buttons one for continue and another for logout.

 

Flow:
1. At the ime of page load a timer will be invoked and it will execute at every 10 sec (you can set it by your own).
2. At every movement of mouse or click or keyborad press it will track the last action time on that page.
3. On the basis of last action performed it will calculate the idle time and if the idle time is greater than warning time then it will show the message box.
4. If user remain inactive then after count down it will atomatically take the user to logout page.
5. If user clicks continue then session will be stay active and it will send a request at server side to wake up server session also.

Download:
https://www.nuget.org/packages/ClientSideSessionTimeout/

Direct Install in your project:
PM> Install-Package ClientSideSessionTimeout



Instructions:
Written in a text file inside Instruction folder, you can get this after installing.

Friday, August 7, 2015

Backup and Restore current session

Problem:

I was doing impersonation in my project, from one login user can insert into another person's account this is for admin mainly. here I am facing one problem when the user logged out from 2nd account then I need to repopulate the previous session of the current user. To do so I backed up the current session and store that in a single key after logged in with 2nd user and after log out from 2nd user I get the key and remove current session then restore the session.


Solution:

Backup session store that in a dictionary-
         public Dictionary<string, object> BackupSession()
        {
            Dictionary<string, object> dicSesson = new System.Collections.Generic.Dictionary<string, object>();

            for (int i = 0; i <= HttpContext.Current.Session.Count - 1; i++)
            {
                dicSesson.Add(HttpContext.Current.Session.Keys[i],  HttpContext.Current.Session[i]);
            }

            return dicSesson;
        }


Restore session keys from the dictionary-
        public void RestoreSession(Dictionary<string, object> prevSessionData)
        {
            foreach (string key in prevSessionData.Keys)
            {
                HttpContext.Current.Session[key] = prevSessionData[key];
            }
        }