从控制台调用Rails控制器(传递凭证和/或会话ID)

时间:2021-11-21 21:33:16

app.get '/namespace/controller'

app.get /命名空间/控制器的

works, but how can I:

有效,但我如何:

  1. Pass the credentials to a secure controller.
  2. 将凭据传递给安全控制器。
  3. Pass the SESSIONID for a previously authenticated user.
  4. 为以前经过身份验证的用户传递SESSIONID。

1 个解决方案

#1


4  

The app object takes care of the session for you, so you don't need to worry about handling the session id manually. So, you just login, then access other pages that depend on the session being set:

app对象为您处理会话,所以您不必担心手动处理会话id。因此,您只需登录,然后访问依赖于会话的其他页面:

app.post app.login_path, :username => "username", :password => "password"
app.get app.other_path

You can also inspect the response and session after any request, eg.:

您也可以在任何请求之后检查响应和会话,例如:

app.response.redirect_url
app.session[:user_id]       # assumes your login process uses the `user_id` key in your session

If you want, you can create a file called .irbrc in your $HOME directory, combining some of these ideas with something like this in it:

如果你愿意,你可以在你的$HOME目录中创建一个名为.irbrc的文件,将其中的一些想法与如下内容结合起来:

if Rails
  def login
    app.post app.login_path, :username => "username", :password => "password"
    "Logged in with user_id: #{app.session[:user_id]}"
  end
end

Then, in your Rails console you can just type login to issue that method, and it will return the string showing the value of the user_id session element.

然后,在Rails控制台中,只需键入login就可以发出该方法,它将返回显示user_id会话元素值的字符串。

#1


4  

The app object takes care of the session for you, so you don't need to worry about handling the session id manually. So, you just login, then access other pages that depend on the session being set:

app对象为您处理会话,所以您不必担心手动处理会话id。因此,您只需登录,然后访问依赖于会话的其他页面:

app.post app.login_path, :username => "username", :password => "password"
app.get app.other_path

You can also inspect the response and session after any request, eg.:

您也可以在任何请求之后检查响应和会话,例如:

app.response.redirect_url
app.session[:user_id]       # assumes your login process uses the `user_id` key in your session

If you want, you can create a file called .irbrc in your $HOME directory, combining some of these ideas with something like this in it:

如果你愿意,你可以在你的$HOME目录中创建一个名为.irbrc的文件,将其中的一些想法与如下内容结合起来:

if Rails
  def login
    app.post app.login_path, :username => "username", :password => "password"
    "Logged in with user_id: #{app.session[:user_id]}"
  end
end

Then, in your Rails console you can just type login to issue that method, and it will return the string showing the value of the user_id session element.

然后,在Rails控制台中,只需键入login就可以发出该方法,它将返回显示user_id会话元素值的字符串。