自定义Mvc5 Owin 验证

 public class AuthIn : IUserAuthenticate
    {
        public static ApplicationUserManager UserManager
        {
            get { return HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); }
        }

        private IAuthenticationManager AuthenticationManager
        {
            get { return HttpContext.Current.GetOwinContext().Authentication; }
        }


        public void CurUserLoginOut()
        {
            AuthenticationManager.SignOut();
        }

        public bool GetUserIsAuthenticated()
        {
            return HttpContext.Current.User.Identity.IsAuthenticated;
        }

        public void SignUserLogin(string strUserName, Dictionary<string, string> extDatas)
        {
            ApplicationUser user = UserManager.FindByName(strUserName);

            if (user == null)
            {
                user = new ApplicationUser { UserName = strUserName, Email = extDatas["Email"] };

                IdentityResult result = Task.Factory.StartNew(s =>
                {
                    return ((ApplicationUserManager)s).CreateAsync(user);
                }, UserManager).Unwrap().GetAwaiter().GetResult();


                if (!result.Succeeded)
                {
                    HttpContext.Current.Response.Write("Error on Create User");
                    return;
                }
            }


            ClaimsIdentity indentiy = Task.Factory.StartNew(s =>
            {
                return user.GenerateUserIdentityAsync(((ApplicationUserManager)s));
            }, UserManager).Unwrap().GetAwaiter().GetResult();


            AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = false }, indentiy);
        }
    }
posted @ 2014-08-13 10:56  张保维  阅读(2161)  评论(0编辑  收藏  举报