【问题标题】:app.UseOAuthBearerTokens with ASP.NET Identity 2.0's DbContext middleware?app.UseOAuthBearerTokens 与 ASP.NET Identity 2.0 的 DbContext 中间件?
【发布时间】:2014-01-12 16:53:49
【问题描述】:

编辑:进步后,我可以缩小问题的范围:

应该对 VS2013 SPA 模板(使用 ASP.NET 身份 1.0)中的 startup.auth.cs 和 ApplicationOAuthProvider.cs 进行哪些更改才能将其迁移到使用 ASP.NET 身份 2.0?

编辑 2:我进一步简化了这个问题。如何将 app.UseOAuthBearerTokens 与 ASP.NET Identity 2.0 的中间件一起使用来检索 DbContext?

        app.UseOAuthBearerTokens(new Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerOptions()
            {
                //What goes here??
            });

(可用的示例中没有这方面的示例。)

Asp.net 身份框架从 V1.0 到 V2.0alpha 存在显着差异。有一个可用的示例说明如何使用 V2:

https://aspnet.codeplex.com/SourceControl/latest (参见示例->身份->ChangePK)

但该示例不是 MVC 或 SPA。话虽如此,我有一个从 VS2013 ASP.NET SPA 应用程序(包含 Identity 1.0)构建的应用程序。我一直在尝试在我的 MVC 应用程序中实现示例中的代码,但我不清楚 VS2013 SPA 模板中的哪些代码被删除以支持示例中的代码。

问另一种方式,有没有人有在 ASP.NET MVC 应用程序中实现 ASP.NET identity 2.0 alpha 的指导? (理想情况下,有从利用身份 1.0 的 VS2013 MVC SPA 模板迁移的步骤)

【问题讨论】:

    标签: asp.net-mvc entity-framework visual-studio-2013 asp.net-identity owin


    【解决方案1】:

    如果您正在寻找如何为 WEBAPI 和 MVC Cookie 身份验证实现 Bearer 令牌,请查看这篇文章:

    ASP.NET Identity 2.0 Cookie & Token Authentication including a sample project.

    简单地说,这个解决方案使用 OWIN 中间件组件 UseOAuthBearerAuthenticationUseCookieAuthentication(我知道 Cookie 身份验证不是问题的一部分,但与 MVC 项目非常相关)来支持基于浏览器的身份验证和 WEBAPI 分别通过 CookiesTokens 请求身份验证。

    Startup.Auth.cs

    OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
    
    //This will used the HTTP header: "Authorization" Value: "Bearer 1234123412341234asdfasdfasdfasdf"
    app.UseOAuthBearerAuthentication(OAuthBearerOptions);
    // Enable the application to use a cookie to store information for the signed in user
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login")
    }); 
    

    HostAuthenticationFilter 表示通过 OWIN 中间件进行身份验证的身份验证过滤器:

    WebApiConfig.cs

    config.SuppressDefaultHostAuthentication();
    //This will used the HTTP header: "Authorization" Value: "Bearer 1234123412341234asdfasdfasdfasdf"
    config.Filters.Add(new HostAuthenticationFilter("Bearer"));
    

    生成令牌:

    var identity = new ClaimsIdentity(Startup.OAuthBearerOptions.AuthenticationType);
    identity.AddClaim(new Claim(ClaimTypes.Name, user));
    identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userIdentity.Id));
    AuthenticationTicket ticket = new AuthenticationTicket(identity, new AuthenticationProperties());
    var currentUtc = new SystemClock().UtcNow;
    ticket.Properties.IssuedUtc = currentUtc;
    ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(30));
    string AccessToken = Startup.OAuthBearerOptions.AccessTokenFormat.Protect(ticket);
    return AccessToken;
    

    【讨论】:

      【解决方案2】:

      以下只是 SPA 模板中的代码,其中将 UserManager 的提供程序替换为 2.0 Identity 中引入的内容。

      OAuthOptions = new OAuthAuthorizationServerOptions
                  {
                      TokenEndpointPath = new PathString("/Token"),
                      Provider = new ApplicationOAuthProvider(PublicClientId, () => HttpContext.Current.GetOwinContext().Get<ApplicationUserManager>()),
                      AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
                      AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
                      AllowInsecureHttp = false
                  };
      

      这里还有一个您可以使用的通用 ApplicationOauthProvider: https://gist.github.com/s093294/9076631 (请注意我没有测试它,只是为你把它放在一起)

      如果您有以下示例:

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

      你可以的

              OAuthOptions = new OAuthAuthorizationServerOptions
              {
                  TokenEndpointPath = new PathString("/Token"),
                  Provider = new ApplicationOAuthProvider<ApplicationUserManager,ApplicationUser,Guid>(PublicClientId),
                  AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
                  AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
                  AllowInsecureHttp = false
              };
      

      【讨论】:

        猜你喜欢
        • 2016-07-21
        • 1970-01-01
        • 2013-11-23
        • 1970-01-01
        • 2018-12-31
        • 2015-06-17
        • 2014-12-14
        • 2018-01-15
        • 1970-01-01
        相关资源
        最近更新 更多