after await next when I try to use serilog LogContext it doesn’t push any property to log messages - asp.net-core

While setting everything for Serilog and SEQ I was stuck in an issue that you may have an answer for it.
I am trying to add some properties to all logs using LogContext.PushProperty. However, in my middleware(see image below) the LogContext can push property to logs before await next.Invoke(). While after await next when I try to use LogContext it doesn’t push any property to log messages.
The issue is that claims are always empty before the await next.Invoke() and they only have values after await next so I am forced to use LogContext after the await but it doesn’t work there as mentioned. Please advise if you have a clue?
Thanks,

LogContext needs to be used in conjunction with a using block, covering the whole scope in which you want the property to be available. You may need something like:
IDisposable popContext = null;
var user2 = context.User as IAMClaimsUser;
if (user2 != null && !string.IsNullOrEmpty(user2.FirstName))
{
popContext = LogContext.PushProperty("UserEmail", user2.Email);
}
using (popContext)
{
// `UserEmail` will be attached to events logged in this block
await next();
}

Related

Asp.Net Core Identity: GenerateChangePhoneNumberTokenAsync not creating AspNetUserTokens record

I'm generating phone number token using GenerateChangePhoneNumberTokenAsync() after creating the user using UserManager. I consistently see that GenerateChangePhoneNumberTokenAsync() does not create a record in AspNetUserTokens table even though the token is generated. Phone verification fails because of this.
I also call GenerateEmailConfirmationTokenAsync() to verify the email. But email verification is successful even though there is no record in AspNetUserTokens table. Can anyone shed light on why GenerateChangePhoneNumberTokenAsync not persisting AspNetUserTokens
record?
Platform: .Net 5
Snippet below:
using (var scope = TransactionUtil.CreateAsyncTransactionScope())
{
var result = await _userManager.CreateAsync(user, createUserDto.Password).ConfigureAwait(false);
if (!result.Succeeded)
{
throw new Exception("Unable to create account");
}
// Business logic removed for clarity
scope.Complete();
}
await SendConfirmEmailAsync(user);
await SendPhoneNumberTokenAsync(user);
private async Task SendConfirmEmailAsync(ApplicationUser user)
{
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
// Send EMail
}
private async Task SendPhoneNumberTokenAsync(ApplicationUser user)
{
if(user == null || string.IsNullOrEmpty(user.PhoneNumber) || user.PhoneNumberConfirmed)
return;
var token = await _userManager.GenerateChangePhoneNumberTokenAsync(user, user.PhoneNumber);
// Send SMS
}
I think we might have a little misunderstood here...
Both GenerateChangePhoneNumberTokenAsync and GenerateEmailConfirmationTokenAsync won't storing anything up to database.
As the code implementation that userManager.GenerateChangePhoneNumberTokenAsync would call to GenerateUserTokenAsync as we can see here, that's the same method GenerateChangePhoneNumberTokenAsync would call (only difference in params).
GenerateUserTokenAsync then call to GenerateAsync method that both PhoneNumberTokenProvider and EmailTokenProvider was just simply take from the base class TotpSecurityStampBasedTokenProvider, which again, not storing anything up to database, we can check manager.CreateSecurityTokenAsync method here if having any doubt.
The AspNetUserTokens token that mentioned above (which i believe was implemented on EF) was designed to store informations about external authentication token storage, which was already answered here.

DownstreamContext of context not found

I'm trying to add a custom middleware to add parameters to the query.
According to some online search, you can do something like this:
{
//PreQueryStringBuilderMiddleware occurs after authorization
PreQueryStringBuilderMiddleware = async (ctx, next) =>
{
var upstreamRoute = ctx.DownstreamReRoute.UpstreamPathTemplate;
Log.Information($"{upstreamRoute}");
await next.Invoke();
}
};
See this answer
But for me it says HttpContext doesn't contain a definition of DownstreamReRoute.
Is there something I am missing or has this been changed?
Alright, turned out this did actually change and in an earlier versions this still works. But it looks like Ocelot is dead anyway from what I read.
var upstreamRoute = ctx.Items.DownstreamRoute().UpstreamPathTemplate
Use above code instead of
var upstreamRoute = ctx.DownstreamReRoute.UpstreamPathTemplate;
Such change was done between 15.0.6 and 16.0.0 Ocelot versions.
For details you can check them on github: https://github.com/ThreeMammals/Ocelot/compare/15.0.6...16.0.0

recursive lambda function never seems to run

I'm not familiar enough with node.js or lambda to see an obvious solution to a dilemma I have. I'm writing some utilities on lambda to manipulate images in an S3 bucket and make them accessible via the GatewayAPI to rest calls.
BACKGROUND DETAILS:
One of the utilities I have retrieves the headObject information such as the mtime, size and metadata. The images themselves will likely be coming in from various means and I won't always have control over adding metadata to them when they arrive/are-created. But I don't really need it until it's necessary to view details about the image from a web interface. And when I do that, I use a thumbnail instead so I created a lambda create-event triggered script (and also have a fall back variation of it via the gatewayAPI) that will create a thumbnail (either when the image is first uploaded to S3 or whenever I make the gateway CreateThumbbnail call) at which time it adds metadata to the thumbnail for the image with things like the original image mimetype, pixel width and height.
What I would like to be able to do, is to create a 'GetObjectInfo' that firsts pulls the headObject data, then checks to see if the bucket specified is or is not the bucket with the associated thumbnail files. (e.g. if it is or is not a thumbnail object) If it is 'not' a thumbnail, I want to then go retrieve -- or at least attempt to retrieve -- the headObject for the associated thumbnail file and attach the thumbnail file's metadata (if the thumbnail exists) onto the data from the original head request before returning the information.
The problem is, that when I set up an async callback scheme, the first headObject request completes, the second never seems to get out of the starting gate.
The method in my class is:
getHeadObject(bucket,object,callback) {
console.log(bucket, "CLASS-head#1")
this.s3.headObject({"Bucket":bucket,"Key":object}, function(err,data){
console.log(bucket, "CLASS-head#2")
callback(err,data)
})
}
getObjectInfo(bucket,object,callback) {
let scope = this
console.log(bucket,"CLASS-object#1")
this.getHeadObject(bucket,object,function(err,data) {
console.log(bucket,"CLASS-object#2")
if(err)
callback(err,data)
else
callback(null,data)
})
}
The lambda code that calls it recursively is:
var cInst = new myClass()
cInst.getObjectInfo(srcBucket,filePath,function(err,data) {
if(data.status == 1) { // if parent request success
// if parent is not thumbnail
if(srcBucket != THUMB_BUCKET) { // see if a thumbnail exists
let thumbPath = myClass.getThumbPath(srcBucket,userId,directory,targetObject)
console.log('---- thumbPath', thumbPath)
cInst.getObjectInfo(THUMB_BUCKET,thumbPath, function(err,thumbData) {
console.log("thumbData #1",thumbData)
if(thumbData.status == 1) { // thumbnail exists
console.log("thumbData")
}
})
}
context.succeed(myClass.createResponse(1, data, api))
} else {
context.fail(myClass.createResponse(data.status, data, api))
}
})
First call on the parent is see
{bucket} "CLASS-object#1"
{bucket} "CLASS-head#1"
{bucket} "CLASS-head#2"
{bucket} "CLASS-object#2"
on the second I only see:
image-thumbnails "CLASS-object#1"
image-thumbnails "CLASS-head#1"
(getThumbPath is just a static utility function that builds the thumbnail path based on the parameters related to the original file. It is already tested as working and produces something like {original-bucket-name}/{userid}/{subdirectory}/{file-basename_150x150.jpg} for any given image - I confirmed that in this instance, the thumbnail exists and matches the path returned by getThumbPath and the acl appears to have permission to read the bucket and the object)
UPDATE: More weirdness
I tried setting the permissions to publicly readable on the thumbnail and it worked. So I started messing with the acl. For the time being since I am still testing, I just gave the role for the scripts full S3 permissions.
But I noticed now that it's working and not working intermittently. One time it completes, the next time it doesn't. WTF is going on here?
I would bet that this is the most common problem that people see when using Node.js with Lambda.
When a Node.js Lambda reaches the end of the main thread, it ends all other threads. When it reaches the end of the handler, it stops all concurrent promises or async calls that are running.
To make sure that the lambda does not prematurely terminate those threads, wait until those promises are complete by using await.
In your case, the following will work: wrap any async calls in a promise and then await them.
await new Promise(async (resolve, reject) => {
cInst.getObjectInfo(srcBucket,filePath,function(err,data) {
if(data.status == 1) {
if(srcBucket != THUMB_BUCKET) {
...
...
await new Promise((resolve2, reject2) => {
cInst.getObjectInfo(THUMB_BUCKET,thumbPath, function(err,thumbData) {
...
...
resolve2();
})
})
}
context.succeed(myClass.createResponse(1, data, api))
resolve();
} else {
context.fail(myClass.createResponse(data.status, data, api))
reject();
}
})
})

ExpressJS - res.status(500) returning undefined so I can't call send on it

I have some code that usually works, but twice now has produced the following error:
TypeError: Cannot read property 'send' of undefined
The code is:
app.user.get('/status.json', mGatewayTimeout, function (req, res) {
var user = req.user
var qs = cu.querystring.parseUrl(req.url)
if (user.apps && user.apps.beeminder && user.apps.beeminder.access_token) {
if (bsCache[user.username] && !qs.force) {
res.send(bsCache[user.username])
} else {
var bee = new Bee({access_token: req.user.apps.beeminder.access_token})
bee.getUserSkinny(function (err, bm_user) {
if (err) {
bsCache[user.username] = null
return res.status(500).send('error: ' + err.toString())
So that last line produces the TypeError when it tries to call .send on res.status(500).
I've left in a whole bunch of stuff that is certainly irrelevant, because if I tried to take out everything that I thought was irrelevant, we'd be left with nothing.
Bee is an API client, but one that I wrote, so I'm not sure it isn't doing something weird, except that I can't see how it could be affecting the response object.
Try
res.status(500).send(`error ${err.message}`),
new Error's are objects made from a constructor and one of the properties is message.
Oh man, I'm a fool. I made my own problem here. This was on an endpoint that accesses an external API, and since I'm on heroku I need all requests to take under 30s, so I wrote some timeout code that would send an error then turn various functions like res.send into no-op functions. Problem, of course, was that I didn't proceed to return the res object in those no-ops.
🤦‍♀️

How do you configure a client to auto-answer within vLine?

What is the correct method for setting a client to auto answer with the vLine API for WebRTC calls?
Looking at your comment, it looks like you have figured this out. But for completeness and for future reference I will go ahead and answer.
To auto answer a call, all you have to do is call MediaSession.start() when an incoming call comes in, instead of throwing a prompt to the user.
Here is an example snippet:
client.on('add:mediaSession', onAddMediaSession, self);
// Handle new media sessions
onAddMediaSession(event){
var mediaSession = event.target;
mediaSession.on('enterState:incoming', onIncoming, self);
},
// Handle new incoming calls and autoAccept
onIncoming(event){
var mediaSession = event.target;
// Auto Accept call instead of a prompt
mediaSession.start();
}
Note that you can do this in your code even if you are using the UI Widgets.