.Net Core 2.1 Web API using JWT and JWT Cookie at the same time












-2















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

























  • 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 '18 at 14:32


















-2















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

























  • 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 '18 at 14:32
















-2












-2








-2








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 Nov 19 '18 at 10:35







DoubleVoid

















asked Nov 15 '18 at 13:38









DoubleVoidDoubleVoid

380629




380629













  • 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 '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 '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 '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 '18 at 14:32














1 Answer
1






active

oldest

votes


















2





+100









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
    Nov 19 '18 at 10:18













  • 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
    Nov 19 '18 at 10:19











  • 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
    Nov 19 '18 at 10:33






  • 1





    @DoubleVoid here's one option: docs.microsoft.com/en-us/aspnet/core/mvc/controllers/…

    – gldraphael
    Nov 22 '18 at 14:31











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',
autoActivateHeartbeat: false,
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









2





+100









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
    Nov 19 '18 at 10:18













  • 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
    Nov 19 '18 at 10:19











  • 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
    Nov 19 '18 at 10:33






  • 1





    @DoubleVoid here's one option: docs.microsoft.com/en-us/aspnet/core/mvc/controllers/…

    – gldraphael
    Nov 22 '18 at 14:31
















2





+100









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
    Nov 19 '18 at 10:18













  • 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
    Nov 19 '18 at 10:19











  • 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
    Nov 19 '18 at 10:33






  • 1





    @DoubleVoid here's one option: docs.microsoft.com/en-us/aspnet/core/mvc/controllers/…

    – gldraphael
    Nov 22 '18 at 14:31














2





+100







2





+100



2




+100





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 Nov 19 '18 at 6:09









KahbaziKahbazi

6,09312146




6,09312146













  • 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
    Nov 19 '18 at 10:18













  • 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
    Nov 19 '18 at 10:19











  • 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
    Nov 19 '18 at 10:33






  • 1





    @DoubleVoid here's one option: docs.microsoft.com/en-us/aspnet/core/mvc/controllers/…

    – gldraphael
    Nov 22 '18 at 14:31



















  • 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
    Nov 19 '18 at 10:18













  • 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
    Nov 19 '18 at 10:19











  • 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
    Nov 19 '18 at 10:33






  • 1





    @DoubleVoid here's one option: docs.microsoft.com/en-us/aspnet/core/mvc/controllers/…

    – gldraphael
    Nov 22 '18 at 14:31

















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
Nov 19 '18 at 10:18







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
Nov 19 '18 at 10:18















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
Nov 19 '18 at 10:19





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
Nov 19 '18 at 10:19













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
Nov 19 '18 at 10:33





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
Nov 19 '18 at 10:33




1




1





@DoubleVoid here's one option: docs.microsoft.com/en-us/aspnet/core/mvc/controllers/…

– gldraphael
Nov 22 '18 at 14:31





@DoubleVoid here's one option: docs.microsoft.com/en-us/aspnet/core/mvc/controllers/…

– gldraphael
Nov 22 '18 at 14:31




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




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

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values