簡體   English   中英

具有Owin JWT身份的MVC

[英]MVC with Owin JWT Identity

我正在嘗試弄清楚如何從代幣中提出索賠。 我會盡量簡短的解釋

  • 我有一個HTML頁面,該頁面向我的Web api進行發布,並進行身份驗證並返回JWT令牌
  • 當我拿回令牌時,我想將其發送到其他網址,而我使用的方式是使用querystring。 我知道我可以使用cookie,但是對於此應用程序,我們不想使用它們。 因此,如果我的網址看起來像這樣http://somedomain/checkout/?token=bearer token comes here

我正在使用Owin middleware ,這就是到目前為止

app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
            {
                Provider = new ApplicationOAuthBearerAuthenticationProvider(),
            });

public class ApplicationOAuthBearerAuthenticationProvider
            : OAuthBearerAuthenticationProvider
        {

            public override Task RequestToken(OAuthRequestTokenContext context)
            {
                if (context == null)
                    throw new ArgumentNullException("context");

                var token = HttpContext.Current.Request.QueryString["token"];
                if (!string.IsNullOrEmpty(token))
                    context.Token = token;
                return Task.FromResult<object>(null);
            }
        }

但是我如何從Token獲取Claims或只是檢查IsAuthenticated

我在controller內嘗試了Following只是為了檢查,但IsAuthenticated始終為false

var identity = (ClaimsIdentity) HttpContext.Current.GetOwinContext().Authentication.User.Identity;
  if (!identity.IsAuthenticated)
      return;

  var id = identity.FindFirst(ClaimTypes.NameIdentifier);

好的,所以我設法弄清楚了。 上面的代碼運行良好,但是我需要添加UseJwtBearerAuthentication中間件。

我最終從原始代碼更改的一件事是更改了context.Token = token; context.Request.Headers.Add("Authorization", new[] { string.Format("Bearer {0}", token) });

所以我的啟動課程看起來像這樣...

public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
            {
                Provider = new ApplicationOAuthBearerAuthenticationProvider(),
            });
            app.UseJwtBearerAuthentication(JwtOptions());

            ConfigureAuth(app);
        }


        private static JwtBearerAuthenticationOptions JwtOptions()
        {
            var key = Encoding.UTF8.GetBytes(ConfigurationManager.AppSettings["auth:key"]);
            var jwt = new JwtBearerAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Active,
                TokenValidationParameters = new TokenValidationParameters
                {
                    ValidAudience = Some Audience,
                    ValidIssuer = Some Issuer,
                    IssuerSigningToken = new BinarySecretSecurityToken(key),
                    RequireExpirationTime = false,
                    ValidateLifetime = false
                }
            };
            return jwt;
        }

        public class ApplicationOAuthBearerAuthenticationProvider
            : OAuthBearerAuthenticationProvider
        {

            public override Task RequestToken(OAuthRequestTokenContext context)
            {
                if (context == null)
                    throw new ArgumentNullException("context");

                var token = HttpContext.Current.Request.QueryString["token"];
                if (!string.IsNullOrEmpty(token))
                    context.Request.Headers.Add("Authorization", new[] { string.Format("Bearer {0}", token) });
                return Task.FromResult<object>(null);
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM