You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
[Question] http request against authorized api:s and server pre-rendering #249
Hi folks
Have a question in regards to the prerendering functionality. It seems that when prerendering is run it doesnt recognize for example an active forms session through Identity (where the method is utilizing the Authorize attribute). It will render on the server-side, resulting in a status 401, then rendered again at the client-side (this time successfully).
The question is two-fold. 1: Is there any way of making the prerendering functionality take identity authorization into consideration and if not, is there preferred way (other than buildning control mechanism yourself) which can be used to only run certain ng2 calls / methods if rendered on client-side?
The intended way to pass through whatever auth tokens you need into your server-side JavaScript is via the aspnet-prerender-data parameter (see the docs under the heading Passing data from .NET code into JavaScript codehere).
We have considered trying to do some kind of automatic transparent pass-through of cookie data, so that any calls to Node's http get/post APIs automatically have attached whatever cookies originally came into the ASP.NET application. But from a security point of view that's very dangerous - we'd have to reimplement the browser's same-origin policy logic completely and perfectly, plus all the CORS logic that runs in browsers, otherwise you might unexpectedly leak auth credentials to third party API endpoints. We're not realistically going to do that, plus even if we did, it would only account for cookie-based auth, and not any other kind of authentication that's common in SPAs (such as just holding tokens in memory and including them in fetch calls).
So to answer your questions directly:
1: Is there any way of making the prerendering functionality take identity authorization into consideration
Yes, get whatever auth tokens you want in your .NET code, and then pass them through into your boot-server.ts logic via the aspnet-prerender-data API. See the docs linked above for exact details. Then your Node-side code can include it in any API calls you make.
2: is there preferred way (other than buildning control mechanism yourself) which can be used to only run certain ng2 calls / methods if rendered on client-side?
Yes, you can have logic like this:
if (typeof window === 'undefined') {
// code that runs only on the server
} else {
// code that runs only on the client
}
If you have any proposals for how we could make this better then please let us know!
We have considered trying to do some kind of automatic transparent pass-through of cookie data, so that any calls to Node's http get/post APIs automatically have attached whatever cookies originally came into the ASP.NET application.
We went through this same thought process for our own APIs and ended up implementing an isomorphic HTTP request library that uses cookies that were passed in from the C# process when on the server side, but we have to be careful to only pass through cookies for internal requests. As @SteveSandersonMS said this would be either very difficult or very dangerous to implement in a generic way. 😄
Activity
SteveSandersonMS commentedon Aug 15, 2016
The intended way to pass through whatever auth tokens you need into your server-side JavaScript is via the
aspnet-prerender-dataparameter (see the docs under the heading Passing data from .NET code into JavaScript code here).We have considered trying to do some kind of automatic transparent pass-through of cookie data, so that any calls to Node's
httpget/post APIs automatically have attached whatever cookies originally came into the ASP.NET application. But from a security point of view that's very dangerous - we'd have to reimplement the browser's same-origin policy logic completely and perfectly, plus all the CORS logic that runs in browsers, otherwise you might unexpectedly leak auth credentials to third party API endpoints. We're not realistically going to do that, plus even if we did, it would only account for cookie-based auth, and not any other kind of authentication that's common in SPAs (such as just holding tokens in memory and including them infetchcalls).So to answer your questions directly:
Yes, get whatever auth tokens you want in your .NET code, and then pass them through into your
boot-server.tslogic via theaspnet-prerender-dataAPI. See the docs linked above for exact details. Then your Node-side code can include it in any API calls you make.Yes, you can have logic like this:
If you have any proposals for how we could make this better then please let us know!
MarkPieszak commentedon Aug 15, 2016
You can also import isBrowser or isServer from Universal to test whether it's browser / server as well. Just throwing that out there!
SteveSandersonMS commentedon Aug 15, 2016
@MarkPieszak Cool, I didn't know about those. Thanks for the info!
baserz commentedon Aug 16, 2016
Thanks guys! Suspected something like isBrowser, isServer, just didnt find it. If i have any useful input / suggestions ill post it :).
bradchristensen commentedon Aug 21, 2016
We went through this same thought process for our own APIs and ended up implementing an isomorphic HTTP request library that uses cookies that were passed in from the C# process when on the server side, but we have to be careful to only pass through cookies for internal requests. As @SteveSandersonMS said this would be either very difficult or very dangerous to implement in a generic way. 😄