Esempio n. 1
0
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (
                HttpContext.Current.Session["User"] == null
                )
            {
                var authenticatedCookie = httpContext.Request.Cookies[OpenIdMembershipService.LOGIN_COOKIE_NAME];
                if (authenticatedCookie != null)
                {
                    var authenticatedCookieValue = authenticatedCookie.Value.ToString();
                    if (!string.IsNullOrWhiteSpace(authenticatedCookieValue))
                    {
                        var user = OpenIdUser.FromCookieString(authenticatedCookieValue);

                        if (user != null && user.IsActive)
                        {
                            HttpContext.Current.Session.Add("User", user);
                        }
                        else
                        {
                            HttpCookie myCookie = new HttpCookie(OpenIdMembershipService.LOGIN_COOKIE_NAME);
                            myCookie.Expires = DateTime.Now.AddDays(-1d);
                            httpContext.Response.Cookies.Add(myCookie);
                        }
                    }
                }
            }
            else
            {
                OpenIdUser sessionUser = (OpenIdUser)HttpContext.Current.Session["User"];
                User       databaseUser;
                using (AllUsersRepository allUserRep = new AllUsersRepository())
                {
                    databaseUser = allUserRep.GetEntity(sessionUser.UserId);
                }

                if (databaseUser != null)
                {
                    sessionUser.Roles = databaseUser.Roles;
                }

                if (databaseUser == null || !databaseUser.IsActive)
                {
                    HttpContext.Current.Session.Remove("User");

                    HttpCookie myCookie = new HttpCookie(OpenIdMembershipService.LOGIN_COOKIE_NAME);
                    myCookie.Expires = DateTime.Now.AddDays(-1d);
                    HttpContext.Current.Response.Cookies.Add(myCookie);
                }
            }
            if (HttpContext.Current.Session["User"] != null)
            {
                //Create culture info object

                CultureInfo ci = new CultureInfo(((OpenIdUser)HttpContext.Current.Session["User"]).LanguageCode);
                System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
                System.Threading.Thread.CurrentThread.CurrentCulture   =
                    CultureInfo.CreateSpecificCulture(ci.Name);
            }
            return(HttpContext.Current.Session["User"] != null);
        }