Ejemplo n.º 1
0
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //取得 ASP.NET 使用者
            var user = System.Web.HttpContext.Current.User;

            //是否通過驗證
            if (user?.Identity?.IsAuthenticated == true)
            {
                //取得 FormsIdentity
                var identity = (FormsIdentity)user.Identity;

                //取得 FormsAuthenticationTicket
                var ticket = identity.Ticket;

                //將 Ticket 內的 UserData 解析回 User 物件
                UserInfo = CommonModel.DecodeJWTTokenMVC(ticket.UserData);
            }
        }
Ejemplo n.º 2
0
        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            //取得 ASP.NET 使用者
            var user = HttpContext.Current.User;

            if (user != null)
            {
                //是否通過驗證
                if (user?.Identity?.IsAuthenticated == true)
                {
                    //取得 FormsIdentity
                    var identity = (FormsIdentity)user.Identity;

                    //取得 FormsAuthenticationTicket
                    var UserData = identity.Ticket.UserData;
                    UserInfo = CommonModel.DecodeJWTTokenMVC(UserData);

                    string[] roles = UserInfo.OperRole.Split(new char[] { ',' });

                    Context.User = new GenericPrincipal(Context.User.Identity, roles);
                }
            }
        }