Monday, February 3, 2014

How to get visitor IP address in static method?

        protected static string GetUserIPAddress()
        {
            string VisitorsIPAddr = string.Empty;
            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
            {
                VisitorsIPAddr = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else if (System.Web.HttpContext.Current.Request.UserHostAddress.Length != 0)
            {
                VisitorsIPAddr = System.Web.HttpContext.Current.Request.UserHostAddress;
            }
            return VisitorsIPAddr;
        }

Thursday, January 30, 2014

How to fix problem: In ASP.NET form authentication, cookie only working under compatibility mode in IE 10 or above?

In web.config file, Add cookieless="UseCookies" property into Form Authentication section
<authentication mode="Forms">
<forms cookieless="UseCookies" loginurl="/" name=".AUTH" path="/" timeout="10000">
</forms></authentication>
Reference:
http://stackoverflow.com/questions/6983732/ie10-user-agent-causes-asp-net-to-not-send-back-set-cookie-ie10-not-setting-coo

Wednesday, January 29, 2014

How to get index for foreach binding in Knockout?

                




The Syntax for get parent data index is:
$parentContext.$index

Tuesday, January 28, 2014

How to create AllowAnonymous Attribute in ASP.NET MVC 3?

There is one in ASP.NET MVC 4, but not in MVC 3.
    [AttributeUsage(AttributeTargets.Class |
AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public sealed class AllowAnonymousAttribute : Attribute
    {
    }