码迷,mamicode.com
首页 > Windows程序 > 详细

自定义Mvc5 Owin 验证

时间:2014-08-13 12:32:36      阅读:918      评论:0      收藏:0      [点我收藏+]

标签:http   io   ar   art   cti   log   new   res   

 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   io   ar   art   cti   log   new   res   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!