微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有unity.mvc的Identity AuthenticationManager

如何解决带有unity.mvc的Identity AuthenticationManager

我正在对使用身份进行登录的现有asp.net MVC网站实施依赖项注入。

User.Identity.IsAuthenticated始终为假

我安装了NuGet软件包:Install-Package Unity.Mvc

在Unity.Config添加了以下代码

container.RegisterType<ApplicationDbContext>(new HierarchicalLifetimeManager());
                container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
                container.RegisterType<IUserStore<ApplicationUser>,UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
    
                var accountInjectionConstructor = new InjectionConstructor(new ApplicationDbContext());
                container.RegisterType<IUserStore<ApplicationUser>,UserStore<ApplicationUser>>(accountInjectionConstructor);
    
                container.RegisterType<IAuthenticationManager>(new InjectionFactory(o => HttpContext.Current.GetowinContext().Authentication));

AccountController构造函数

private readonly UserManager<ApplicationUser> UserManager;
            private readonly IAuthenticationManager AuthenticationManager;   
     public AccountController(IUserStore<ApplicationUser> userStore,IAuthenticationManager authManager)
                {
                    UserManager = new UserManager<ApplicationUser>(userStore);
                    AuthenticationManager = authManager;
                }

登录方法

private async Task SignInAsync(ApplicationUser user,bool isPersistent)
            {
                AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                var identity = await UserManager.CreateIdentityAsync(user,DefaultAuthenticationTypes.ApplicationCookie); //identity.IsAuthenticated is true
                AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent },identity); //User.Identity.IsAuthenticated is false
            }

问题: 请检查上面的注释行identity.IsAuthenticated为true(可以),但User.Identity.IsAuthenticated仍然为false。

如何在HttpContext.User.Identity中更新此身份?

解决方法

尚未调用android { configurations.all { resolutionStrategy.force 'com.android.support:support-annotations:27.1.1' } } 中的

ConfigureAuth()。调用此功能可解决问题

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。