app.UseOAuthBearerTokens与ASP.NET Identity 2.0的DbContext中间件?

Ben*_*iFB 2 asp.net-mvc entity-framework owin visual-studio-2013 asp.net-identity

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

应该对VS2013 SPA模板中的startup.auth.cs和ApplicationOAuthProvider.cs进行哪些更改(使用ASP.NET标识1.0),以便将其迁移到使用ASP.NET标识2.0?

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

        app.UseOAuthBearerTokens(new Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerOptions()
            {
                //What goes here??
            });
Run Code Online (Sandbox Code Playgroud)

(样本中没有这样的例子.)

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

https://aspnet.codeplex.com/SourceControl/latest (参见Samples-> Identity-> 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模板迁移的步骤)

hyl*_*er0 8

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

ASP.NET Identity 2.0 Cookie和令牌身份验证,包括示例项目.

简单地说,该解决方案采用了OWIN中间件组件UseOAuthBearerAuthenticationUseCookieAuthentication(我知道cookie认证是不是问题的一部分,但对于MVC项目非常相关的),以支持基于浏览器的认证和的WebAPI通过请求认证饼干令牌分别.

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")
}); 
Run Code Online (Sandbox Code Playgroud)

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

WebApiConfig.cs

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

生成令牌:

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;
Run Code Online (Sandbox Code Playgroud)