Translate

Tuesday 25 March 2014

Get Client IP address using javascript

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>

    <script language="javascript" type="text/javascript">
    function GetClientIp()
        {
            var ip= '<%= Request.ServerVariables["REMOTE_ADDR"] %>';
            alert("Your IP Address is :"+ip+" ");
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="GetIP" runat="server" Text="Get IP Address" OnClientClick="GetClientIp();" />
    </div>
    </form>
</body>
</html>

-----------------------------------------------------------

Server Variables collection:

VariableDescription
ALL_HTTPReturns all HTTP headers sent by the client. Always prefixed with HTTP_ and capitalized
ALL_RAWReturns all headers in raw form
APPL_MD_PATHReturns the meta base path for the application for the ISAPI DLL
APPL_PHYSICAL_PATHReturns the physical path corresponding to the meta base path
AUTH_PASSWORDReturns the value entered in the client's authentication dialog
AUTH_TYPEThe authentication method that the server uses to validate users
AUTH_USERReturns the raw authenticated user name
CERT_COOKIEReturns the unique ID for client certificate as a string
CERT_FLAGSbit0 is set to 1 if the client certificate is present and bit1 is set to 1 if the cCertification authority of the client certificate is not valid
CERT_ISSUERReturns the issuer field of the client certificate
CERT_KEYSIZEReturns the number of bits in Secure Sockets Layer connection key size
CERT_SECRETKEYSIZEReturns the number of bits in server certificate private key
CERT_SERIALNUMBERReturns the serial number field of the client certificate
CERT_SERVER_ISSUERReturns the issuer field of the server certificate
CERT_SERVER_SUBJECTReturns the subject field of the server certificate
CERT_SUBJECTReturns the subject field of the client certificate
CONTENT_LENGTHReturns the length of the content as sent by the client
CONTENT_TYPEReturns the data type of the content
GATEWAY_INTERFACEReturns the revision of the CGI specification used by the server
HTTP_<HeaderName>Returns the value stored in the header HeaderName
HTTP_ACCEPTReturns the value of the Accept header
HTTP_ACCEPT_LANGUAGEReturns a string describing the language to use for displaying content
HTTP_COOKIEReturns the cookie string included with the request
HTTP_REFERERReturns a string containing the URL of the page that referred the request to the current page using an <a> tag. If the page is redirected, HTTP_REFERER is empty
HTTP_USER_AGENTReturns a string describing the browser that sent the request
HTTPSReturns ON if the request came in through secure channel or OFF if the request came in through a non-secure channel
HTTPS_KEYSIZEReturns the number of bits in Secure Sockets Layer connection key size
HTTPS_SECRETKEYSIZEReturns the number of bits in server certificate private key
HTTPS_SERVER_ISSUERReturns the issuer field of the server certificate
HTTPS_SERVER_SUBJECTReturns the subject field of the server certificate
INSTANCE_IDThe ID for the IIS instance in text format
INSTANCE_META_PATHThe meta base path for the instance of IIS that responds to the request
LOCAL_ADDRReturns the server address on which the request came in
LOGON_USERReturns the Windows account that the user is logged into
PATH_INFOReturns extra path information as given by the client
PATH_TRANSLATEDA translated version of PATH_INFO that takes the path and performs any necessary virtual-to-physical mapping
QUERY_STRINGReturns the query information stored in the string following the question mark (?) in the HTTP request
REMOTE_ADDRReturns the IP address of the remote host making the request
REMOTE_HOSTReturns the name of the host making the request
REMOTE_USERReturns an unmapped user-name string sent in by the user
REQUEST_METHODReturns the method used to make the request
SCRIPT_NAMEReturns a virtual path to the script being executed
SERVER_NAMEReturns the server's host name, DNS alias, or IP address as it would appear in self-referencing URLs
SERVER_PORTReturns the port number to which the request was sent
SERVER_PORT_SECUREReturns a string that contains 0 or 1. If the request is being handled on the secure port, it will be 1. Otherwise, it will be 0
SERVER_PROTOCOLReturns the name and revision of the request information protocol
SERVER_SOFTWAREReturns the name and version of the server software that answers the request and runs the gateway
URLReturns the base portion of the URL

Session Management

The following are the available Session State Modes in ASP.NET:
  • InProc
  • StateServer
  • SQLServer
  • Custom
  • Off
InProc Session State Mode

The InProc Session State Mode is the default Session State Mode. We can host multiple websites/web applications on a single IIS. Each application runs in a separate Application Domain. The InProc Session State Mode stores session data in a memory object in the application worker process (aspnet_wp.exe) in the application domain. It is usually the fastest, but more session data means more memory is used on the web server, and that can affect performance. 

The session data is stored in the application domain of the web server. When the server restarts then existing data is lost. If you modify the Global.asax file and the Web.Config file for an ASP.NET application then the application will be restarted and all the session data will be lost.

SP.NET provides two events that help you manage user sessions. These events are defined in the Global.asax file of the web application.
Sr. NoEventCall
1Session_Start()This event occurs when a new session begins.
2Session_End()This event occurs when a session is abandoned or expires.

<configuration>
  <
system.web>    
    <
sessionState mode="InProc" timeout="25"></sessionState>
  </
system.web></configuration>

The preceding session timeout setting keeps the session alive for 25 minutes. If you don't define a timeout attribute in the SessionState then the default value is 20 minutes.

Advantages of InProc Session State Mode

Here is a list of the advantages of the InProc Session State Mode.
  1. It is easy to implement.
  2. It stores the session data on the server so it is fast.
  3. In this mode, it is not necessary to serialize and de-serialize to store and retrieve data in the session variables.
Disadvantages of InProc Session State Mode

Here is a list of the disadvantages of the InProc Session State Mode.
  1. When the application domain or worker process recycles, the session data will be lost.
  2. It is usually the fastest, but more session data means more memory is used on the web server, and that can affect performance
  3. It won't work in web farm scenarios and web garden scenarios, because in these scenarios multiple "aspnet_wp.exe" processes will be running on the same machine.
Conclusion
The InProc Session State Mode is a very fast session storing mechanism but suitable only for small web applications. InProc session data would be lost if we restart the server or if the application domain is recycled.
------------------------------------------------------------------------------------------------------


ASP.NET Application and Page Life Cycle

Creation of ASP.NET Environment

Step 1: The user sends a request to IIS. IIS first checks which ISAPI extension can serve this request. Depending on file extension the request is processed. For instance, if the page is an ‘.ASPX page’, then it will be passed to ‘aspnet_isapi.dll’ for processing.

Step 2: If this is the first request to the website, then a class called as ‘ApplicationManager’ creates an application domain where the website can run. As we all know, the application domain creates isolation between two web applications hosted on the same IIS. So in case there is an issue in one app domain, it does not affect the other app domain.

Step 3: The newly created application domain creates hosting environment, i.e. the ‘HttpRuntime’ object. Once the hosting environment is created, the necessary core ASP.NET objects like ‘HttpContext’ , ‘HttpRequest’ and ‘HttpResponse’ objects are created.

Step 4: Once all the core ASP.NET objects are created, ‘HttpApplication’ object is created to serve the request. In case you have a ‘global.asax’ file in your system, then the object of the ‘global.asax’ file will be created. Please noteglobal.asax file inherits from ‘HttpApplication’ class.
Note: The first time an ASP.NET page is attached to an application, a new instance of ‘HttpApplication’ is created. Said and done to maximize performance, HttpApplication instances might be reused for multiple requests.

Step 5: The HttpApplication object is then assigned to the core ASP.NET objects to process the page.

Step 6: HttpApplication then starts processing the request by HTTP module events, handlers and page events. It fires the MHPM event for request processing.
Note: For more details, read this.
The below image explains how the internal object model looks like for an ASP.NET request. At the top level is the ASP.NET runtime which creates an ‘Appdomain’ which in turn has ‘HttpRuntime’ with ‘request’, ‘response’ and ‘context’ objects.

-------------------------------------------------------------------------------------------------------
You can read more about the differences from here.
Below is the logical flow of how the request is processed. There are 4 important steps MHPM as explained below:

Step 1(M: HttpModule): Client request processing starts. Before the ASP.NET engine goes and creates the ASP.NETHttpModule emits events which can be used to inject customized logic. There are 6 important events which you can utilize before your page object is created BeginRequestAuthenticateRequestAuthorizeRequest,ResolveRequestCacheAcquireRequestState and PreRequestHandlerExecute.

Step 2 (H: ‘HttpHandler’): Once the above 6 events are fired, ASP.NET engine will invoke ProcessRequest event if you have implemented HttpHandler in your project.

Step 3 (P: ASP.NET page): Once the HttpHandler logic executes, the ASP.NET page object is created. While the ASP.NET page object is created, many events are fired which can help us to write our custom logic inside those page events. There are 6 important events which provides us placeholder to write logic inside ASP.NET pages InitLoad,validateeventrender and unload. You can remember the word SILVER to remember the events S – Start (does not signify anything as such just forms the word) , I – (Init) , L (Load) , V (Validate), E (Event) and R (Render).

Step4 (M: HttpModule): Once the page object is executed and unloaded from memory, HttpModule provides post page execution events which can be used to inject custom post-processing logic. There are 4 important post-processing events PostRequestHandlerExecuteReleaserequestStateUpdateRequestCache and EndRequest.
The below figure shows the same in a pictorial format.