.Net Core 2.1 Web API using JWT and JWT Cookie at the same time
up vote
-3
down vote
favorite
I would like to store the JWT in a cookie and use it to authenticate the user OR the bearer token from the HTTP header.
At the moment I am using HTTP-Auth header only and it's working.
I tried using Identity Cookies and JwT like this:
[Authorize] //Cookie auth?!
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class ValuesController : ControllerBase
Startup.cs:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(...)
...
services.AddIdentity<ApplicationUser, IdentityRole>()
I also tried adding different Schemes in AddAuthentication()
. It's not working.
My Question is how to activate JWT and ASP.NET Identity Authentication simultaneously for Actions/Controllers.
c# asp.net-core asp.net-core-webapi
This question has an open bounty worth +100
reputation from DoubleVoid ending in 3 days.
This question has not received enough attention.
add a comment |
up vote
-3
down vote
favorite
I would like to store the JWT in a cookie and use it to authenticate the user OR the bearer token from the HTTP header.
At the moment I am using HTTP-Auth header only and it's working.
I tried using Identity Cookies and JwT like this:
[Authorize] //Cookie auth?!
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class ValuesController : ControllerBase
Startup.cs:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(...)
...
services.AddIdentity<ApplicationUser, IdentityRole>()
I also tried adding different Schemes in AddAuthentication()
. It's not working.
My Question is how to activate JWT and ASP.NET Identity Authentication simultaneously for Actions/Controllers.
c# asp.net-core asp.net-core-webapi
This question has an open bounty worth +100
reputation from DoubleVoid ending in 3 days.
This question has not received enough attention.
What is the question? You got downvoted because as it's currently written, it’s hard to tell exactly what you're asking. Please clarify your specific problem or add additional details to highlight exactly what you need
– Roman Marusyk
Nov 18 at 14:32
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I would like to store the JWT in a cookie and use it to authenticate the user OR the bearer token from the HTTP header.
At the moment I am using HTTP-Auth header only and it's working.
I tried using Identity Cookies and JwT like this:
[Authorize] //Cookie auth?!
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class ValuesController : ControllerBase
Startup.cs:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(...)
...
services.AddIdentity<ApplicationUser, IdentityRole>()
I also tried adding different Schemes in AddAuthentication()
. It's not working.
My Question is how to activate JWT and ASP.NET Identity Authentication simultaneously for Actions/Controllers.
c# asp.net-core asp.net-core-webapi
I would like to store the JWT in a cookie and use it to authenticate the user OR the bearer token from the HTTP header.
At the moment I am using HTTP-Auth header only and it's working.
I tried using Identity Cookies and JwT like this:
[Authorize] //Cookie auth?!
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class ValuesController : ControllerBase
Startup.cs:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(...)
...
services.AddIdentity<ApplicationUser, IdentityRole>()
I also tried adding different Schemes in AddAuthentication()
. It's not working.
My Question is how to activate JWT and ASP.NET Identity Authentication simultaneously for Actions/Controllers.
c# asp.net-core asp.net-core-webapi
c# asp.net-core asp.net-core-webapi
edited 2 days ago
asked Nov 15 at 13:38
DoubleVoid
365628
365628
This question has an open bounty worth +100
reputation from DoubleVoid ending in 3 days.
This question has not received enough attention.
This question has an open bounty worth +100
reputation from DoubleVoid ending in 3 days.
This question has not received enough attention.
What is the question? You got downvoted because as it's currently written, it’s hard to tell exactly what you're asking. Please clarify your specific problem or add additional details to highlight exactly what you need
– Roman Marusyk
Nov 18 at 14:32
add a comment |
What is the question? You got downvoted because as it's currently written, it’s hard to tell exactly what you're asking. Please clarify your specific problem or add additional details to highlight exactly what you need
– Roman Marusyk
Nov 18 at 14:32
What is the question? You got downvoted because as it's currently written, it’s hard to tell exactly what you're asking. Please clarify your specific problem or add additional details to highlight exactly what you need
– Roman Marusyk
Nov 18 at 14:32
What is the question? You got downvoted because as it's currently written, it’s hard to tell exactly what you're asking. Please clarify your specific problem or add additional details to highlight exactly what you need
– Roman Marusyk
Nov 18 at 14:32
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Based on your description I'm guessing you need more than one authentication on your actions.
You can add multiple Authentication Schema which you did with AddJwtBearer
and AddIdentity
, and on AuthorizeAttribute
choose multiple schema.
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + ", " + Microsoft.AspNetCore.Identity.IdentityConstants.ApplicationScheme)]
Yes, this is what I am looking for. But this gives me an error:An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
I tried wrapping this up in a static readonly string. Doesn't work either;[Authorize(AuthenticationSchemes = AuthType.Value)]
– DoubleVoid
2 days ago
Oh and I really wanted to know how to store the JWT in a cookie and use this for authorization + Normal Microsoft JWT Authorization. But combining Identity + JWT should be okay too, as long as both work simultaneously.
– DoubleVoid
2 days ago
Changed it to"Bearer, Identity.Application"
and it seems to work :) I just need a way to simplify it, so I don't have to put[Authorize(AuthenticationSchemes = "Bearer, Identity.Application")]
above each Action/Controller. Any ideas?
– DoubleVoid
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Based on your description I'm guessing you need more than one authentication on your actions.
You can add multiple Authentication Schema which you did with AddJwtBearer
and AddIdentity
, and on AuthorizeAttribute
choose multiple schema.
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + ", " + Microsoft.AspNetCore.Identity.IdentityConstants.ApplicationScheme)]
Yes, this is what I am looking for. But this gives me an error:An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
I tried wrapping this up in a static readonly string. Doesn't work either;[Authorize(AuthenticationSchemes = AuthType.Value)]
– DoubleVoid
2 days ago
Oh and I really wanted to know how to store the JWT in a cookie and use this for authorization + Normal Microsoft JWT Authorization. But combining Identity + JWT should be okay too, as long as both work simultaneously.
– DoubleVoid
2 days ago
Changed it to"Bearer, Identity.Application"
and it seems to work :) I just need a way to simplify it, so I don't have to put[Authorize(AuthenticationSchemes = "Bearer, Identity.Application")]
above each Action/Controller. Any ideas?
– DoubleVoid
2 days ago
add a comment |
up vote
1
down vote
accepted
Based on your description I'm guessing you need more than one authentication on your actions.
You can add multiple Authentication Schema which you did with AddJwtBearer
and AddIdentity
, and on AuthorizeAttribute
choose multiple schema.
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + ", " + Microsoft.AspNetCore.Identity.IdentityConstants.ApplicationScheme)]
Yes, this is what I am looking for. But this gives me an error:An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
I tried wrapping this up in a static readonly string. Doesn't work either;[Authorize(AuthenticationSchemes = AuthType.Value)]
– DoubleVoid
2 days ago
Oh and I really wanted to know how to store the JWT in a cookie and use this for authorization + Normal Microsoft JWT Authorization. But combining Identity + JWT should be okay too, as long as both work simultaneously.
– DoubleVoid
2 days ago
Changed it to"Bearer, Identity.Application"
and it seems to work :) I just need a way to simplify it, so I don't have to put[Authorize(AuthenticationSchemes = "Bearer, Identity.Application")]
above each Action/Controller. Any ideas?
– DoubleVoid
2 days ago
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Based on your description I'm guessing you need more than one authentication on your actions.
You can add multiple Authentication Schema which you did with AddJwtBearer
and AddIdentity
, and on AuthorizeAttribute
choose multiple schema.
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + ", " + Microsoft.AspNetCore.Identity.IdentityConstants.ApplicationScheme)]
Based on your description I'm guessing you need more than one authentication on your actions.
You can add multiple Authentication Schema which you did with AddJwtBearer
and AddIdentity
, and on AuthorizeAttribute
choose multiple schema.
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + ", " + Microsoft.AspNetCore.Identity.IdentityConstants.ApplicationScheme)]
answered 2 days ago
Kahbazi
5,66312043
5,66312043
Yes, this is what I am looking for. But this gives me an error:An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
I tried wrapping this up in a static readonly string. Doesn't work either;[Authorize(AuthenticationSchemes = AuthType.Value)]
– DoubleVoid
2 days ago
Oh and I really wanted to know how to store the JWT in a cookie and use this for authorization + Normal Microsoft JWT Authorization. But combining Identity + JWT should be okay too, as long as both work simultaneously.
– DoubleVoid
2 days ago
Changed it to"Bearer, Identity.Application"
and it seems to work :) I just need a way to simplify it, so I don't have to put[Authorize(AuthenticationSchemes = "Bearer, Identity.Application")]
above each Action/Controller. Any ideas?
– DoubleVoid
2 days ago
add a comment |
Yes, this is what I am looking for. But this gives me an error:An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
I tried wrapping this up in a static readonly string. Doesn't work either;[Authorize(AuthenticationSchemes = AuthType.Value)]
– DoubleVoid
2 days ago
Oh and I really wanted to know how to store the JWT in a cookie and use this for authorization + Normal Microsoft JWT Authorization. But combining Identity + JWT should be okay too, as long as both work simultaneously.
– DoubleVoid
2 days ago
Changed it to"Bearer, Identity.Application"
and it seems to work :) I just need a way to simplify it, so I don't have to put[Authorize(AuthenticationSchemes = "Bearer, Identity.Application")]
above each Action/Controller. Any ideas?
– DoubleVoid
2 days ago
Yes, this is what I am looking for. But this gives me an error:
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
I tried wrapping this up in a static readonly string. Doesn't work either; [Authorize(AuthenticationSchemes = AuthType.Value)]
– DoubleVoid
2 days ago
Yes, this is what I am looking for. But this gives me an error:
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
I tried wrapping this up in a static readonly string. Doesn't work either; [Authorize(AuthenticationSchemes = AuthType.Value)]
– DoubleVoid
2 days ago
Oh and I really wanted to know how to store the JWT in a cookie and use this for authorization + Normal Microsoft JWT Authorization. But combining Identity + JWT should be okay too, as long as both work simultaneously.
– DoubleVoid
2 days ago
Oh and I really wanted to know how to store the JWT in a cookie and use this for authorization + Normal Microsoft JWT Authorization. But combining Identity + JWT should be okay too, as long as both work simultaneously.
– DoubleVoid
2 days ago
Changed it to
"Bearer, Identity.Application"
and it seems to work :) I just need a way to simplify it, so I don't have to put [Authorize(AuthenticationSchemes = "Bearer, Identity.Application")]
above each Action/Controller. Any ideas?– DoubleVoid
2 days ago
Changed it to
"Bearer, Identity.Application"
and it seems to work :) I just need a way to simplify it, so I don't have to put [Authorize(AuthenticationSchemes = "Bearer, Identity.Application")]
above each Action/Controller. Any ideas?– DoubleVoid
2 days ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320729%2fnet-core-2-1-web-api-using-jwt-and-jwt-cookie-at-the-same-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the question? You got downvoted because as it's currently written, it’s hard to tell exactly what you're asking. Please clarify your specific problem or add additional details to highlight exactly what you need
– Roman Marusyk
Nov 18 at 14:32