Can't get $Session variables in custom inputs endpoints [non-reproducible]


I’m trying to get Session variables in the endpoint of a custom inputs but I can’t get it to work.
To explain in further detais:
- I have my dashboard organized as a folder of pages, an init file (where i manage the login, get the pages from the folder mentionned previously and start the dashboard) and an api.
- In the init file, after loging in, I store the username and ID of the user in $Session variables. They both are retrievable from every endpoint in the dashboard except the endpoints of custom inputs.
- When I try to submit my input, I get an error: Unable to link the argument to the parameter “Identity”, because it is Null (sorry for the syntax, I have the french version: Impossible de lier l’argument au paramètre “Identity”, car il a la valeur Null)

Does any one of you know of this or has an idea of why I can’t do it ?
Thanks in advance for your help !

I’m not sure what you mean by custom input, but I’m not aware of the issue either.
Please can you post some code so that we can see what your input & endpoint look like?

Custom inputs:

In the initiation of the dashboard, I store some info about the user in $Session:

$FormLogin = New-UDAuthenticationMethod -Endpoint {
$users | ForEach-Object {
    if ($Credentials.UserName -eq $user.username -and $passHash -eq $user.hash) {
        $Role = "User"
        if ($user.admin -eq 1) {
            $Role = "Administrator"
        $Session:User = $user.username
        $Session:Name = $user.prenom
        $Session:Id = $user.userID
        New-UDAuthenticationResult -Success -UserName $User -Role $Role
New-UDAuthenticationResult -ErrorMessage "Identifiants incorrects"

And in a custum input’s endpoint, I try to retrieve them:

$NewVM = New-UDPage -Name "NewVM" -AuthorizedRole "Administrator" -Endpoint {
New-UDInput -Title "Nouvelle VM" -Id "new_vm" -SubmitText "Créer" -Content {
    New-UDInputField -Type 'textbox' -Name ...
} -Endpoint {
    $Token = Grant-UDJsonWebToken -UserName $Session:User -SigningKey "pX-T9UhEgsakhaEJ#^YJd?T*CMK?_R#fmTG!hNJ2"
    $api_url = "http://localhost:10002/api/user/" + $Session:Id

But I get the error aforementioned…

$session:username = $user

New-UDInput -Title "Test input" -Id "Testinput" -SubmitText "submit" -Content {
    New-UDInputField -Type 'textbox' -Name 'Test' -Placeholder 'Test'
} -Endpoint {
    Show-UDToast -Message "$test $($session:username)" -Duration 5000

The above works fine for me inside a page -endpoint so i dont think the issue is the component here, I beleive that your $session variables are likley not set as you expect them to be?

Why dont you try the below on its own and see if that produces anything?
new-udhtml -markup "Session var: $($Session:User)"

I imagine that where you’re setting the session variable, inside the authentication block is the issue, maybe before the $session var has fully initialised?
Out of curiousity, I can see you are pulling the info directly from the $user variable, so why pass it through to a session variable? why not just use it directly from $user within your endpoint?

I thank you for your time and insight.
And I’m sorry but the problem seems to be gone overnight. I don’t see any explanation about why it was there in the first place and even less concerning how it disappeared…
To answer your question, I understood that I needed to store it in global variable in order to be able to access it from other endpoints… And I may have tried to use $user but failed to do so but I can’t remember.
I have a question though. Why do you put your Session:... in ( ) ?

-markup is taking a string

Within " " singular variables are evaluated, but not within ’ ’ (which is a literal string).
However, if you try to put anything else in there, notice the following does not evaluate:

" $ "
" $session:property "

So the way around this, is to encase it with $($session:property), powershell basically evaluates whats inside these brackets first (its a SubExpression), and returns the result as a variable into the string. Hopefully that makes sense :slight_smile:

More info here

1 Like

Ok thank you ! Makes perfect sense :slight_smile:

Actually I’m wrong, its just been force of habit. The logic applies to sub properties of variables, so the first example " $ " you would need to wrap it as a subexpression for it to evaluate. However semicolons are evaluated properly, in the same way a standard variable is and doesnt require a subexpression :slight_smile:

There are other uses, such as if you require a variable but directly next to other characters:

one will behave and one wont :+1:

1 Like