.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.










share|improve this question

















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

















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.










share|improve this question

















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















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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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














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)]





share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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)]





share|improve this answer





















  • 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















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)]





share|improve this answer





















  • 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













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)]





share|improve this answer












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)]






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen