Hi everyone !
Im new to UD and im trying to make authentication on a REST API. It doesn’t want to work and im really confused about how it work and how to get the JSON WEB TOKEN working.
I hope you guys can help me…
So i have a very simple dashboard with a login page. Then i have a simple imput field where you can enter names to create new AD users.
The action to create a new account is made by my API. At this point all is working properly. But when i want to use authentication it doesn’t work anymore. I tried to solve it with the docs but actually im really confused.
Here the code of my dashboard :
Enable-UDLogging
$AuthMethod = New-UDAuthenticationMethod -Endpoint {
param([PSCredential]$Credentials)
if ($Credentials.UserName -eq "lucas" -and $Credentials.GetNetworkCredential().Password -eq "Password") {
New-UDAuthenticationResult -Success -UserName "lucas"
}
elseif ($Credentials.UserName -eq "jean" -and $Credentials.GetNetworkCredential().Password -eq "Password") {
New-UDAuthenticationResult -Success -UserName "jean"
}
else{
New-UDAuthenticationResult -ErrorMessage "You are not authorized to enter this dashboard"
}
}
$FormLogin = New-UDLoginPage -AuthenticationMethod $AuthMethod
import-module @("ActiveDirectory", "UniversalDashboard")
$MyDashboard = New-UDDashboard -Title "Dashboard" -LoginPage $FormLogin -Content {
New-UDHeading -Text "Logged in as $User"
New-UDInput -Title Account -Content{
New-UDInputField -Type textbox -Name "nom" -Placeholder "Nom"
New-UDInputField -Type textbox -Name "prenom" -Placeholder "Prenom"
} -Endpoint {
param(
$nom,
$prenom
)
$Token = Invoke-RestMethod "http://localhost:8081/api/login" -Method Post -Body @{
UserName = "lucas"
Password = "Password"
}
Invoke-RestMethod -Uri "http://localhost:8081/api/account/$nom/$prenom" -Method Post -Headers @{Authorization = "Bearer $($Token.token)"}
}
}
Start-UDDashboard -Dashboard $MyDashboard -Name 'Dashboard' -Port 8080 -AllowHttpForLogin
And here the code of my API :
Enable-UDLogging
$AuthMethod = New-UDAuthenticationMethod -Endpoint {
param([PSCredential]$Credentials)
if ($Credentials.UserName -eq "lucas" -and $Credentials.GetNetworkCredential().Password -eq "Password") {
$Token = Grant-UDJsonWebToken -Identity "lucas"
New-UDAuthenticationResult -Success -UserName "lucas" -Token $Token
}
else{
New-UDAuthenticationResult -ErrorMessage "You are not authorized to perform this action !"
}
}
import-module @("ActiveDirectory", "UniversalDashboard")
$Endpoint = New-UDEndpoint -Url "/account/:nom/:prenom" -Method "POST" -Endpoint {
$name = $nom + " " + $prenom
New-ADUser -Surname $nom -GivenName $prenom -Name $name
}
Start-UDRestApi -Endpoint $Endpoint -Port 8081 -Force -AuthenticationMethod $AuthMethod
Apologies for my English.
Thank you by advance for your help.