Background
Today’s world is the world of the internet. Developers build websites using different frameworks, using different technologies. ASP.Net MVC (Model – View – Controller) is one of the powerful frameworks to develop web applications. Nowadays, the internet is a medium where you can do anything like purchasing items, booking tickets, online money transactions etc. So, for every transaction through a website or any message exchange, SECURITY should be very important. Any third person shouldn’t be able to access user’s private/sensible information (could be anything like Passwords, Session IDs, Account Numbers etc.) over HTTP network.
Basically, a website is designed/developed for end users to interact with different web pages, manipulate user data in the form of Request-Response. This Request-Response exchange happens through “HTTP” protocol, and as you all know HTTP is stateless protocol.
Suppose a particular user requested a page (with MVC, a View), server provides that page putting information in HTTP header & forget about that request. But, what if the user again requests for the same page with some data? The server won’t understand the request since, the previous request will be flushed out & server will consider that request, a fresh request.
Introducing HTTP Cookie
To avoid the above situation, ASP.Net MVC has a HTTP Cookie. General definition says, Cookie is a small piece of data sent from a website and stored in the user's web browser while the user is browsing. Cookies are key-value pairs. When developer wants some data to be used for multiple requests with the same user, they use “cookies” with which, the data will be stored on the client side.
In such situation, server sends HTTP header containing the field “Set-Cookie” which holds Key-Value pair with other options to client. Reply to that, client sends request to server along with its own header field which contains name & value of stored cookies to let server know about it. In that case, server will understand that client already has stored cookies, soserver won’t send cookies for next request-response to client unless any changes to cookie values will be there.

Problem
This data stored in client machine. That means, when calling web methods like post and get, all cookie data should be validated. At the same time, you’ll have to ensure that you won’t be storing sensitive information, like passwords, because, cookies are basically text files, anyone can easily pickup those data, modify it, read your sensible information. If the connection between server & client is not over SSL, then it will not be protected from the third-party attackers.
Session cookies are same as any other cookie. But at the server side, that is the only way to have the ID (we consider this as session ID) value, where this ID will be used to fetch data stored on the server. Suppose, third-party attacker senses your data (like ID) via unsecure “HTTP” header which has your cookie information, now he has the ID, so he can send that ID as cookie information with new HTTP header to server. Because of that, server will accept the request & won’t be able to understand whether request come from actual user or from any third-party attacker. Server will assume that as a normal request & will send data related with that ID.
Because attacker sent with new HTTP header, the response comes to that attacker with related information of that ID.

HTTPS with SSL
Before looking into actual solution, if you want the user data to be secured, you have to add SSL certificate & run the website with “HTTPS” protocol.
Solution 1
Adding configuration in web.config
In “web.config”, there are many elements. One of the element called “httpCookies” allows the application to use ReqiresSSL attribute by which, all the transmission of cookies including session will be happened through SSL only. It can also be used inside forms authentication, so if you use SSL for “httpCookies” you must also use it for forms authentication too.
Set the following in Web.config:
- <httpCookies requireSSL="true" />
- <authentication mode="Forms">
- <forms requireSSL="true" />
- </authentication>
Code change for HttpCookie
ASP.Net provides a property to secure the HTTP cookie to be encrypted & send/receive in a secure way. Even if, third person attacks & tries to sense the data in cookie, he won’t be able to decrypt it since the website uses SSL medium.
To do that, we have to set 2 variables& check their values:



Aashish JainPosted Nov 26, 2019, 11:31 PM
Hi Ashish, We receving error while this Object reference not set to an instance of an object. at WebApplication.UserAuthenticationFilter.SslRequest.OnAuthorization(AuthorizationContext authContext)
Aashish JainPosted Nov 26, 2019, 11:30 PM
Hi Ashish ,
Aashish JainPosted Nov 26, 2019, 7:16 AM
Thanks for solution I was apply this on my project and fixed my issue.
Aashish JainPosted Nov 26, 2019, 7:13 AM
This cookie does not have the Secure flag set. When a cookie is set with the Secure flag, it instructs the browser that the cookie can only be accessed over secure SSL channels. This is an important security protection for session cookies.
Humayun Kabir MamunPosted May 22, 2016, 7:44 AM
Nice...
Debasis SahaPosted May 17, 2016, 9:52 AM
Nice one..
Bhavik PatelPosted May 17, 2016, 12:28 AM
nice
Sonu ChaudharyPosted May 16, 2016, 3:21 PM
good one...
Pankaj Kumar ChoudharyPosted May 16, 2016, 2:00 PM
Nice Information.......
Debasis SahaPosted May 16, 2016, 1:49 PM
Nice One..