首页 > Windows开发 > 详细

自定义Mvc5 Owin 验证

时间:2014-08-13 12:32:36      阅读:2074      评论:0      收藏:0      [点我收藏+]
 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);
        }
    }

自定义Mvc5 Owin 验证,布布扣,bubuko.com

自定义Mvc5 Owin 验证

原文:http://www.cnblogs.com/zbw911/p/3909514.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!