使用ASP.NET 2.0身份的中间件的DbContext app.UseOAuthBearerTokens? [英] app.UseOAuthBearerTokens with ASP.NET Identity 2.0's DbContext middleware?

查看:1378
本文介绍了使用ASP.NET 2.0身份的中间件的DbContext app.UseOAuthBearerTokens?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After progressing, I can narrow the scope of the question:

应在VS2013 SPA模板startup.auth.cs和ApplicationOAuthProvider.cs什么样的变化,以迁移它使用(使用ASP.NET 1.0的身份),ASP.NET 2.0的身份?

What changes should be made to startup.auth.cs and ApplicationOAuthProvider.cs in the VS2013 SPA template (using ASP.NET identity 1.0) in order to migrate it to use ASP.NET identity 2.0?

编辑2:我甚至进一步简化这个问题。如何才能使用app.UseOAuthBearerTokens与ASP.NET 2.0身份的中间件用于检索的DbContext?

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

(有这就是可用的样本中没有这方面的例子。)

(There's no example of this in the sample that's available.)

有显著差异从V1.0到Asp.net身份框架V2.0alpha。有可用的例子展示了如何使用V2:

There are significant differences from V1.0 to V2.0alpha of the Asp.net identity framework. There is an example available that shows how to use V2:

的https://aspnet.$c$cplex.com/SourceControl/latest
(见样本 - >身份 - > ChangePK)

https://aspnet.codeplex.com/SourceControl/latest (see Samples->Identity->ChangePK)

但例子不是MVC,或SPA。话虽这么说,我已经得到了从VS2013 ASP.NET应用SPA(它集成了身份1.0),内置的应用程序。我一直在努力执行我的MVC应用程序内的样品中的code,但目前还不清楚对我有什么code从VS2013 SPA模板有利于从样品中code被去除。

but that example is not MVC, or SPA. That being said, I've got an app that was built from the VS2013 ASP.NET SPA app (which incorporates Identity 1.0). I've been trying to implement the code in the sample inside my MVC app, but it's unclear to me what code from the VS2013 SPA template is removed in favor of the code from the sample.

问另一种方式,没有任何人有实现ASP.NET身份的ASP.NET MVC应用程序内2.0阿尔法指导? (最好有步骤,从它利用身份VS2013 MVC SPA模板迁移1.0)

推荐答案

如果您正在寻找如何实现承载令牌的的WebAPI 并MVC Cookie身份验证,然后检查了这篇文章:

If you are looking how to implement Bearer tokens for WEBAPI and MVC Cookie authentication then check out this article:

简单地说,该解决方案采用了OWIN中间件组件 UseOAuthBearerAuthentication UseCookieAuthentication (我知道cookie认证是不是一部分这个问题的,但对于MVC项目非常相关的)通过的缓存并支持基于浏览器的认证和的WebAPI 请求验证令牌,分别为。

Simply put, this solution uses the OWIN Middleware components UseOAuthBearerAuthentication and UseCookieAuthentication (I know Cookie auth is not part of the question but very relevant regarding MVC projects) to support browser based authentication and WEBAPI request authentication via Cookies and Tokens, respectively.

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重新presents经由OWIN中间件认证的认证过滤器:

HostAuthenticationFilter represents an authentication filter that authenticates via OWIN middleware:

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;

这篇关于使用ASP.NET 2.0身份的中间件的DbContext app.UseOAuthBearerTokens?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆