Thursday, December 17, 2015

FormAuthentication Logout

        [AllowAnonymous]
        public void Logout()
        {
            FormsAuthentication.SignOut(); 

            FormsAuthentication.RedirectToLoginPage();
        }

Base control to check access

// this will be executed after authorized
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            _Common.eUserAccess access;

            if (_Common.IsUserLoggedIn())   // check if any user logged in
            {
                string controller = filterContext.RouteData.Values["controller"].ToString();
                string action = filterContext.RouteData.Values["action"].ToString();

                // check if logged in user has access to controller/action
                if (_Common.IsUserHasAccess(controller, action))
                {
                    access = _Common.eUserAccess.AccessGranted;
                }
                else
                {
                    access = _Common.eUserAccess.AccessDenied;
                }
            }
            else
            {
                access = _Common.eUserAccess.NotLoggedIn;
            }

            // process request based on user access
            if (access == _Common.eUserAccess.NotLoggedIn)
            {
                filterContext.Result = RedirectToAction("Login", "Access");
            }
            else if (access == _Common.eUserAccess.AccessDenied)
            {
                filterContext.Result = RedirectToAction("NoAccess", "Access");
            }
            else
            {
                base.OnActionExecuting(filterContext);
            }
        }

Avoid Login / Logout Loop in ASP.NET FormAuthentication

If you are using ASP.NET Form Authentication redirect URL, you will come across a situation when you click logout form a page, you will be redirected to login page and your redirect url will be logout. So when you login, you will be immediately logged out since your redirect url is logout. To avoid this check if your redirect url is logout in the login view as follows;

@{
    var redirectURL = FormsAuthentication.GetRedirectUrl(User.Identity.Name, true);

    if (redirectURL == Url.Action("Logout", "User"))
    {
        redirectURL = "";
    }
}

<form id="login-form" action="@Url.Action("Login", "User")" method="post">
   <input type="hidden" value="@redirectURL" name="RedirectURL" />
   ...
</form>

Wednesday, December 9, 2015

MySql prepare historic data for reporting purpose

-- Prepare a table for days to get sequence of numbers from 1 to 32

CREATE TABLE ids (
id INT UNSIGNED AUTO_INCREMENT NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM;

INSERT INTO ids SET id = 1;           #      1      1  
INSERT INTO ids SELECT NULL FROM ids; #      2      2
INSERT INTO ids SELECT NULL FROM ids; #      4      4
INSERT INTO ids SELECT NULL FROM ids; #      8      8
INSERT INTO ids SELECT NULL FROM ids; #     10     16
INSERT INTO ids SELECT NULL FROM ids; #     20     32


set @enddate = "2015-12-01";
set @days = 15;

SELECT date(qb.date) as date, COALESCE(sum(net_amount), 0) as total from invoice qa
right join
(SELECT
(SELECT @enddate FROM invoice qc LIMIT 1) + INTERVAL 1 DAY - INTERVAL a.id DAY AS date
FROM test.ids a
    ) as qb
on date(qa.date) = qb.date
where qb.date > DATE_SUB(@enddate, INTERVAL @days day)
group by qb.date
order by qb.date asc;