asp.net - context.Get() 和它的通用版本有什么区别?

标签 asp.net asp.net-identity owin katana

我正在努力熟悉 OWIN,但有很多事情让我感到困惑。例如,在部分 startup.cs 类中,我通过以下方式注册上下文回调

app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

有什么不同?为什么我们需要那个泛型方法?

我可以得到这样的上下文:
context.Get<ApplicationDbContext>())
context.GetUserManager<ApplicationUserManager>()

Get 和 GetUserManager 方法之间有什么区别?为什么我不能为 ApplicationUserManager 调用 context.Get?

最佳答案

Get<UserManager>之间没有区别和 GetUserManager<UserManager>
这是两者的源代码...

    /// <summary>
    ///     Retrieves an object from the OwinContext using a key based on the AssemblyQualified type name
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="context"></param>
    /// <returns></returns>
    public static T Get<T>(this IOwinContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        return context.Get<T>(GetKey(typeof (T)));
    }

    /// <summary>
    ///     Get the user manager from the context
    /// </summary>
    /// <typeparam name="TManager"></typeparam>
    /// <param name="context"></param>
    /// <returns></returns>
    public static TManager GetUserManager<TManager>(this IOwinContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        return context.Get<TManager>();
    }

关于asp.net - context.Get() 和它的通用版本有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28585624/

相关文章:

c# - WebApi OWIN 相同 token 对两个不同的服务实例有效

asp.net-mvc - 从 OWIN 身份验证中间件考虑 Controller 属性

c# - ASP.Net MVC 5 OAuth2 Facebook 声明未保留。为什么?

asp.net - HTML 电子邮件中的嵌入图像无法在手机上显示

c# - Digg 风格的 Ajax 投票按钮

asp.net-mvc - asp.net Identity 2.0 unity不解析默认用户存储

asp.net-mvc - 使用 Identity 2 和 Entity Framework 6 在 IdentityUserRoles 表上添加额外的列/字段

c# - SqlMembershipProvider : use my own database as user store database

javascript - document.getElementById 在 asp.net 中的 <div> 上失败

c# - 获取所有用户的特定声明?