【问题标题】:Where is Microsoft.AspNet.Identity.Owin.AuthenticationManager in Asp.Net Identity RTM version?Asp.Net Identity RTM 版本中的 Microsoft.AspNet.Identity.Owin.AuthenticationManager 在哪里?
【发布时间】:2014-06-08 19:01:39
【问题描述】:

我已经从here 安装了 AspNet-identity 程序集的夜间版本

RC 版本中的AuthenticationManager 类似乎已从 RTM 版本中消失(Microsoft.AspNet.Identity.Owin.1.0.0-rtm-130914)。

它曾经在 Microsoft.AspNet.Identity.Owin 程序集中,但现在已经不在了。

此类具有以下方法:SignInAsyncCheckPasswordAndSignInAsync,它们用于您在创建具有个人用户帐户身份验证的新 ASP.Net Web 应用程序 MVC 项目时获得的默认项目。

AuthenticationManager 现在在哪里?或者改用什么?

【问题讨论】:

    标签: asp.net asp.net-identity


    【解决方案1】:

    那个类已经消失了,因为它基本上只是添加了生成 ClaimsIdentity 并将其传递给 Owin.Security.IAuthenticationManager 的方法。

    相反,RTM 模板在控制器中有一个 SignIn 方法,看起来像这样:

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

    【讨论】:

    • 如何获取当前用户的用户名? User.Identity.Name 始终为空
    • 这很可能意味着登录出现问题,并且没有声明主体。
    【解决方案2】:

    如果我错了,但 AuthenticationManager 没有被移到

    HttpContext.Current.GetOwinContext().Authentication?

    所以上面的例子现在是:

    private async Task SignInAsync(UserManager<User> manager, User user, bool isPersistent)
    {
        var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
        authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
        var identity = await manager.CreateIdentityAsync(user, DeffaultAuthenticationTypes.ApplicationCookie);
        authenticationManager.SignIn(new AuthenticationProperties(){ IsPersistent = isPersistent }, identity);
    }
    

    请注意,UserManager 似乎不再具有静态方法 CreateIdentityAsync,因此必须从对象实例中触发。

    除非我错过了什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 2013-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多