Skip to main content
Use NetworkCredential class to get clear password
Source Link
maz
  • 1.8k
  • 10
  • 25

Another way is with the Read-Host commandlet. If you ask it to return a SecureString, it will show a password field (as a modal dialog in Sitecore Powershell). You then have to convert the so-called secure string back into a normal string to validate credentials:

(Update: use NetworkCredential class to get clear password.)

$username = [Sitecore.Context]::User.Name
$securePassword$securePass = Read-Host -Prompt 'Please enter your password' -AsSecureString
$bstr$password = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword)
$password =New-Object [System.RuntimeSystem.InteropServicesNet.Marshal]::PtrToStringAuto($bstrNetworkCredential "", $securePass).Password
$isValid = [System.Web.Security.Membership]::ValidateUser($username, $password)

One issue with this method is you can't control the dialog's title. But it's a quick fix.

Update: yet another (IMO better) way using Read-Variable, which sets a regular string and provides more control:

$username = [Sitecore.Context]::User.Name
$password = '' # Clear password var so there's no initial value
Read-Variable -Parameters @{Name="password"; Title="Password"; Editor="Password"} `
    -Description 'Please enter your password to confirm' `
    -Title 'Confirmation' | Out-Null
$isValid = [System.Web.Security.Membership]::ValidateUser($username, $password)

Another way is with the Read-Host commandlet. If you ask it to return a SecureString, it will show a password field (as a modal dialog in Sitecore Powershell). You then have to convert the so-called secure string back into a normal string to validate credentials:

$username = [Sitecore.Context]::User.Name
$securePassword = Read-Host -Prompt 'Please enter your password' -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword)
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
$isValid = [System.Web.Security.Membership]::ValidateUser($username, $password)

One issue with this method is you can't control the dialog's title. But it's a quick fix.

Update: yet another way using Read-Variable, which sets a regular string and provides more control:

$username = [Sitecore.Context]::User.Name
$password = '' # Clear password var so there's no initial value
Read-Variable -Parameters @{Name="password"; Title="Password"; Editor="Password"} `
    -Description 'Please enter your password to confirm' `
    -Title 'Confirmation' | Out-Null
$isValid = [System.Web.Security.Membership]::ValidateUser($username, $password)

Another way is with the Read-Host commandlet. If you ask it to return a SecureString, it will show a password field (as a modal dialog in Sitecore Powershell). You then have to convert the so-called secure string back into a normal string to validate credentials:

(Update: use NetworkCredential class to get clear password.)

$username = [Sitecore.Context]::User.Name
$securePass = Read-Host -Prompt 'Please enter your password' -AsSecureString
$password = (New-Object System.Net.NetworkCredential "", $securePass).Password
$isValid = [System.Web.Security.Membership]::ValidateUser($username, $password)

One issue with this method is you can't control the dialog's title. But it's a quick fix.

Update: yet another (IMO better) way using Read-Variable, which sets a regular string and provides more control:

$username = [Sitecore.Context]::User.Name
$password = '' # Clear password var so there's no initial value
Read-Variable -Parameters @{Name="password"; Title="Password"; Editor="Password"} `
    -Description 'Please enter your password to confirm' `
    -Title 'Confirmation' | Out-Null
$isValid = [System.Web.Security.Membership]::ValidateUser($username, $password)
added 633 characters in body
Source Link
maz
  • 1.8k
  • 10
  • 25

Another way is with the Read-Host commandlet. If you ask it to return a SecureString, it will show a password field (as a modal dialog in Sitecore Powershell). You then have to convert the so-called secure string back into a normal string to validate credentials:

$username = [Sitecore.Context]::User.Name
$securePassword = Read-Host -Prompt 'Please enter your password' -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword)
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
$isValid = [System.Web.Security.Membership]::ValidateUser($username, $password)

One issue with this method is you can't control the dialog's title. But it's a quick fix.

Update: yet another way using Read-Variable, which sets a regular string and provides more control:

$username = [Sitecore.Context]::User.Name
$password = '' # Clear password var so there's no initial value
Read-Variable -Parameters @{Name="password"; Title="Password"; Editor="Password"} `
    -Description 'Please enter your password to confirm' `
    -Title 'Confirmation' | Out-Null
$isValid = [System.Web.Security.Membership]::ValidateUser($username, $password)

Another way is with the Read-Host commandlet. If you ask it to return a SecureString, it will show a password field (as a modal dialog in Sitecore Powershell). You then have to convert the so-called secure string back into a normal string to validate credentials:

$username = [Sitecore.Context]::User.Name
$securePassword = Read-Host -Prompt 'Please enter your password' -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword)
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
$isValid = [System.Web.Security.Membership]::ValidateUser($username, $password)

One issue with this method is you can't control the dialog's title. But it's a quick fix.

Another way is with the Read-Host commandlet. If you ask it to return a SecureString, it will show a password field (as a modal dialog in Sitecore Powershell). You then have to convert the so-called secure string back into a normal string to validate credentials:

$username = [Sitecore.Context]::User.Name
$securePassword = Read-Host -Prompt 'Please enter your password' -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword)
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
$isValid = [System.Web.Security.Membership]::ValidateUser($username, $password)

One issue with this method is you can't control the dialog's title. But it's a quick fix.

Update: yet another way using Read-Variable, which sets a regular string and provides more control:

$username = [Sitecore.Context]::User.Name
$password = '' # Clear password var so there's no initial value
Read-Variable -Parameters @{Name="password"; Title="Password"; Editor="Password"} `
    -Description 'Please enter your password to confirm' `
    -Title 'Confirmation' | Out-Null
$isValid = [System.Web.Security.Membership]::ValidateUser($username, $password)
Source Link
maz
  • 1.8k
  • 10
  • 25

Another way is with the Read-Host commandlet. If you ask it to return a SecureString, it will show a password field (as a modal dialog in Sitecore Powershell). You then have to convert the so-called secure string back into a normal string to validate credentials:

$username = [Sitecore.Context]::User.Name
$securePassword = Read-Host -Prompt 'Please enter your password' -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword)
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
$isValid = [System.Web.Security.Membership]::ValidateUser($username, $password)

One issue with this method is you can't control the dialog's title. But it's a quick fix.