首页 > 代码库 > 自定义Mvc5 Owin 验证
自定义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); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。