MVC with JWT Owin Authentication

I am trying to figure out how to get a requirement from my token. I will try a short explanation

  • I have an HTML page that posts on my api website, and also auth checks and returns a JWT token
  • when I get the token back, I want to send it to a different URL, and the way I do it is to use querystring. I know that I can use cookies, but for this application we do not want to use them. So if my url looks like thishttp://somedomain/checkout/?token=bearer token comes here

I use Owin middleware, and that is what I still have

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);
            }
        }

But how do I get Claimsout Tokenor just checkIsAuthenticated

I tried the following in my controllerjust check, but IsAuthenticatedalwaysfalse

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

  var id = identity.FindFirst(ClaimTypes.NameIdentifier);
+4
1

, . , , , 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);
            }
        }
    }
+2

Source: https://habr.com/ru/post/1616586/


All Articles