Exemplo n.º 1
0
        private void Authorize(object sender, EventArgs e)
        {
            HttpApplication app      = (HttpApplication)sender;
            var             request  = HttpContext.Current.Request;
            var             response = HttpContext.Current.Response;

            if (!HttpContext.Current.SkipAuthorization)
            {
                HttpCookie cookie = request.Cookies[ConfigurationManager.AppSettings["SessionCookieName"]];

                if (cookie == null)
                {
                    // Loginpage
                    UnAuthorized(app);
                }
                else
                {
                    Guid SessionUid = Guid.Empty;

                    if (Guid.TryParse(cookie.Value, out SessionUid))
                    {
                        GetSessionQuery getSessionQuery = new GetSessionQuery()
                        {
                            SessionUID = SessionUid
                        };

                        GetSessionQueryHandler getSessionQueryHandler = new GetSessionQueryHandler(getSessionQuery, new UnityContainer());
                        var session = getSessionQueryHandler.Handle();

                        if (session == null || session.ExpireDate < DateTimeOffset.Now)
                        {
                            // Loginpage
                            UnAuthorized(app);
                        }
                        else
                        {
                            if (session.MandatorUIDs == null || !session.MandatorUIDs.Any())
                            {
                                throw new Exception("No Mandator found to Session");
                            }
                            else
                            {
                                string serverVariableValue = string.Empty;

                                foreach (Guid mandatorUid in session.MandatorUIDs)
                                {
                                    if (string.IsNullOrEmpty(serverVariableValue))
                                    {
                                        serverVariableValue += mandatorUid.ToString();
                                    }
                                    else
                                    {
                                        serverVariableValue += "," + mandatorUid.ToString();
                                    }
                                }

                                request.ServerVariables.Add("MandatorUID", serverVariableValue);
                            }

                            if (session.ExpireDate < DateTimeOffset.Now.AddHours(-1))
                            {
                                session.ExpireDate = DateTimeOffset.Now.AddDays(1);

                                var sessioncookie = response.Cookies[ConfigurationManager.AppSettings["SessionCookieName"]];

                                if (sessioncookie != null)
                                {
                                    sessioncookie.Expires = session.ExpireDate.DateTime;
                                }

                                UpdateSessionQuery updateSessionQuery = new UpdateSessionQuery()
                                {
                                    Session = session
                                };

                                UpdateSessionQueryHandler updateSessionQueryHandler = new UpdateSessionQueryHandler(updateSessionQuery, new UnityContainer());
                                updateSessionQueryHandler.Handle();
                            }
                        }
                    }
                    else
                    {
                        UnAuthorized(app);
                    }
                }
            }
        }
 public GetSessionQueryHandler(GetSessionQuery query, IUnityContainer unityContainer) : base(unityContainer)
 {
     _query = query;
 }