Beispiel #1
0
        void MvcApplication_AuthorizeRequest(object sender, EventArgs e)
        {
            //IIdentity id = Context.User.Identity;
            //if (id.IsAuthenticated)
            //{
            //    var customerService = System.Web.Mvc.DependencyResolver.Current.GetService(typeof(IUserService)) as IUserService;
            //    var userinfo = customerService.GetUserInfo(id.Name);

            //    ResolverHelper.GetResolver<MVCBlog.Service.PostService>();

            //}
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var             formsIdetity    = HttpContext.Current.User.Identity as FormsIdentity;
                MVCBlogIdentity mvcblogIdentity = new MVCBlogIdentity(formsIdetity.Ticket.UserData)
                {
                    AuthenticationType = formsIdetity.AuthenticationType,
                    IsAuthenticated    = formsIdetity.IsAuthenticated,
                    Name   = formsIdetity.Name,
                    Ticket = formsIdetity.Ticket
                };
                //MVCBlogPrincipal mvcblogPrincipal = new MVCBlogPrincipal() { Identity = mvcblogIdentity, };
                UserRole[] roles = mvcblogIdentity.UserData.UserRoles == null ? new UserRole[] { UserRole.作者 } :
                mvcblogIdentity.UserData.UserRoles.ToArray();

                MVCBlogPrincipal mvcblogPrincipal = new MVCBlogPrincipal(mvcblogIdentity, roles);
                HttpContext.Current.User = mvcblogPrincipal;
            }
        }
Beispiel #2
0
 public static UserInfo GetLogInUserInfo()
 {
     try
     {
         MVCBlogIdentity mvcblogidentity = HttpContext.Current.User.Identity as MVCBlogIdentity;
         if (mvcblogidentity != null && mvcblogidentity.IsAuthenticated)
         {
             IUserService userservice = ApplicationContainer.Container.Resolve <IUserService>();
             var          userinfo    = userservice.GetById(mvcblogidentity.UserData.Id);
             return(userinfo);
         }
     }
     catch
     {
         //HttpContext.Current.User = null;
         //HttpContext.Current.Response.Redirect("/Admin/LogIn");
     }
     return(null);
 }
Beispiel #3
0
 public static void UserLogOut()
 {
     try
     {
         if (HttpContext.Current.User.Identity.IsAuthenticated)
         {
             MVCBlogIdentity mvcblogidentity = HttpContext.Current.User.Identity as MVCBlogIdentity;
             if (mvcblogidentity != null)
             {
                 var cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
                 cookie.Expires = DateTime.Now.AddDays(-1);
                 HttpContext.Current.Response.Cookies.Add(cookie);
                 HttpContext.Current.User = null;
             }
         }
     }
     catch
     {
     }
 }