コード例 #1
0
ファイル: UserFilter.cs プロジェクト: luowei98/Hlyt
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            UserModel userModel;

            if (filterContext.Controller.ViewBag.UserModel == null)
            {
                userModel = new UserModel();
                filterContext.Controller.ViewBag.UserModel = userModel;
            }
            else
            {
                userModel = filterContext.Controller.ViewBag.UserModel as UserModel;
            }
            if (filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                HlytUser yiheUser = filterContext.HttpContext.User.GetHlytUser();
                if (userModel != null)
                {
                    userModel.IsUserAuthenticated = yiheUser.IsAuthenticated;
                    userModel.UserName            = yiheUser.DisplayName;
                    userModel.RoleName            = yiheUser.RoleName;
                }
            }

            base.OnActionExecuted(filterContext);
        }
コード例 #2
0
        private void PostAuthenticateRequestHandler(object sender, EventArgs e)
        {
            HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];

            if (IsValidAuthCookie(authCookie))
            {
                var formsAuthentication = DependencyResolver.Current.GetService <IFormsAuthentication>();

                if (authCookie != null)
                {
                    FormsAuthenticationTicket ticket = formsAuthentication.Decrypt(authCookie.Value);
                    var      yiheUser  = new HlytUser(ticket);
                    string[] userRoles = { yiheUser.RoleName };
                    Context.User = new GenericPrincipal(yiheUser, userRoles);
                    formsAuthentication.SetAuthCookie(Context, ticket);
                }
            }
        }