SitecorePowerShell / Console

Sitecore PowerShell Extensions
https://doc.sitecorepowershell.com/
Other
114 stars 70 forks source link

How to Save into Cookie? #917

Closed HelenAtGitHub closed 7 years ago

HelenAtGitHub commented 7 years ago

We are working on Powershell for Sitecore and tried to save cookies. We have tried the samples in the links below but no luck. Could you give any suggestion?

https://stackoverflow.com/questions/5470474/powershell-httpwebrequest-get-method-cookiecontainer-problem https://www.reddit.com/r/PowerShell/comments/54ytye/using_cookies_with_invokerestmethod/

Regards, Helen

michaellwest commented 7 years ago

Hi @HelenAtGitHub could you provide more details about your use case in which cookies are needed? Are you simply trying to Invoke-WebRequest or Invoke-RestMethod with a cookie included?

HelenAtGitHub commented 7 years ago

Hi Michael, We plan to leverage Powershell for Sitecore module to select roles and assign them to users. I wonder if we can save the selected roles into a Cookie so that it will automatically select the same roles next time I open the report. By the way, I noticed that the Powershell report window has been loaded into an iframe. Will that be tricky to save into Cookie? Your suggestion would be much appreciated.

michaellwest commented 7 years ago

@HelenAtGitHub If you are wanting to manage security for users, I'm not clear on how the use of a Cookie would ever come into play.

There are a number of SPE commands to manage roles. Here are a few:

You can add users to a given role like the following:

Add-RoleMember -Identity sitecore\developer -Members "sitecore\michael","sitecore\adam","sitecore\mike"

Are you wanting to remotely add users to roles through the use of a web service call? Are you using a cookie to transmit user/role associations?

HelenAtGitHub commented 7 years ago

Hi Michael, Sorry to make you confused.
The report will list all the users who are not the members of certain roles. We have created a list of roles for sitecore admin to choose before loading the report . To save their time, we plan to store the selected roles into a Cookie so that they will see the same set of roles selected next time to run the report . list roles Regards, Helen

michaellwest commented 7 years ago

I suppose a cookie could work, but perhaps an alternative approach using Sitecore would be to just save an item in the Sitecore tree and use Get-Item to retrieve it.

HelenAtGitHub commented 7 years ago

HI Michael, I want to pass credential to sitecore login page and save the cookie in the sitecore site, but I met error. It said there are no forms in the $login. Could you give me a sample of how to pass credential to sitecore login page? //////////////////////////////////////////////////////////////////////////////////////////////////////// `$login = Invoke-WebRequest -usebasicparsing -Uri 'http://{website}/sitecore/login' -SessionVariable sqlpass return $login return $login.Forms

$login.Forms[0].Fields["UserName"] = '{username}'

$login.Forms[0].Fields["Password"] = '{password}'

$response = Invoke-WebRequest -Uri 'http://{website}/sitecore/shell' -WebSession $sqlpass -Method POST -Body $login

[string]$BaseUri = "http://{website}/sitecore/shell/default.aspx?sc_lang=en" [string]$ContentType = "application/json" $cookie=new-object system.net.cookie $cookie.name = "testmycookie" $cookie.path = "/" $cookie.value = "testmycookie" $cookie.domain = "{website}" $cookie.expires = "01/10/2026 09:15 AM" $session=new-object microsoft.powershell.commands.webrequestsession $session.cookies.add($cookie)

$Headers=@{ }

$JSONResponse = Invoke-RestMethod -URI $BaseURI -Headers $Headers -ContentType $ContentType -Method Get -websession $session

If($JSONResponse) { Return $JSONResponse }` ////////////////////////////////////////////////////////////////////////////////////////////////////////

michaellwest commented 7 years ago

@HelenAtGitHub Unfortunately the task you are trying to perform goes beyond the scope of the Sitecore PowerShell Extensions module.

You may however explore Invoke-JavaScript, a command included with SPE if you wish to execute JavaScript code from SPE. The Sitecore Slack Chat may also have some devs skilled in PowerShell that can help.

AlexKasaku commented 7 years ago

@HelenAtGitHub - You might be able to achieve what you want by using the user profile and storing selected values there. I've blogged a simple example - https://www.kasaku.co.uk/2017/07/19/sitecore-powershell-storing-in-user-profile/

michaellwest commented 7 years ago

Closing do to no activity. Please reopen if you have additional questions.