Ruby on rails 基本RubyonRails身份验证问题
我正在为rails应用程序开发一个基本的身份验证系统。身份验证使用net ldap类验证Active Directory中的帐户信息(此部分工作正常) 然而,我的会话助理似乎有问题。即使ActiveDirectoryUser.authenticate成功,已签名的\u-in帮助程序始终返回false。登录后,脚本重定向到根路径(默认的\u控制器的主路径),然后立即再次重定向回登录路径——这是已签名的\u-in帮助程序返回false的结果 请参阅下面的代码。我错过了什么 谢谢 应用程序\u控制器.rbRuby on rails 基本RubyonRails身份验证问题,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我正在为rails应用程序开发一个基本的身份验证系统。身份验证使用net ldap类验证Active Directory中的帐户信息(此部分工作正常) 然而,我的会话助理似乎有问题。即使ActiveDirectoryUser.authenticate成功,已签名的\u-in帮助程序始终返回false。登录后,脚本重定向到根路径(默认的\u控制器的主路径),然后立即再次重定向回登录路径——这是已签名的\u-in帮助程序返回false的结果 请参阅下面的代码。我错过了什么 谢谢 应用程序\u控制器.
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
end
class DefaultController < ApplicationController
before_filter :signed_in_user
def home
end
private
def signed_in_user
redirect_to signin_path, notice: "Please sign in." unless signed_in?
end
end
module SessionsHelper
def sign_in(user)
@current_user = user
end
def current_user
@current_user ||= nil
end
def signed_in?
!@current_user.nil?
end
def sign_out
@current_user = nil
end
end
class SessionsController < ApplicationController
def new
end
def create
user = ActiveDirectoryUser.authenticate(params[:session][:username],params[:session][:password])
if user.nil?
# authentication failed
flash.now[:error] = 'Invalid email/password combination'
render 'new'
else
# authentication succeeded
sign_in @user
flash[:error] = 'Great success'
redirect_to root_path
end
end
def destroy
sign_out
redirect_to root_path
end
end
会话\u controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
end
class DefaultController < ApplicationController
before_filter :signed_in_user
def home
end
private
def signed_in_user
redirect_to signin_path, notice: "Please sign in." unless signed_in?
end
end
module SessionsHelper
def sign_in(user)
@current_user = user
end
def current_user
@current_user ||= nil
end
def signed_in?
!@current_user.nil?
end
def sign_out
@current_user = nil
end
end
class SessionsController < ApplicationController
def new
end
def create
user = ActiveDirectoryUser.authenticate(params[:session][:username],params[:session][:password])
if user.nil?
# authentication failed
flash.now[:error] = 'Invalid email/password combination'
render 'new'
else
# authentication succeeded
sign_in @user
flash[:error] = 'Great success'
redirect_to root_path
end
end
def destroy
sign_out
redirect_to root_path
end
end
class sessioncontroller
您应该使用session for来持久化这类数据(将针对每个请求进行评估),它是用户数据。但我强烈建议您使用像设计gem这样的东西,它可以为您完成所有认证工作,甚至更多。为什么要重新创造weel权利
我相信这对你有用
module SessionsHelper
def sign_in(user)
session[:user_id] = user.id
end
def current_user
ActiveDirectoryUser.find(session[:user_id]) ||= nil
end
def signed_in?
!session[:user_id].nil?
end
def sign_out
session[:user_id] = nil
end
end
您应该使用session for来持久化这类数据(将对每个请求进行评估),即用户数据。但我强烈建议您使用像设计gem这样的东西,它可以为您完成所有认证工作,甚至更多。为什么要重新创造weel权利 我相信这对你有用
module SessionsHelper
def sign_in(user)
session[:user_id] = user.id
end
def current_user
ActiveDirectoryUser.find(session[:user_id]) ||= nil
end
def signed_in?
!session[:user_id].nil?
end
def sign_out
session[:user_id] = nil
end
end
您如何存储用户已成功登录的事实?我没有看到任何对会话的调用来存储此信息。由于我对rails的理解有限,它不是通过sessions\u controller.rb中的“登录用户”传递的吗?从LDAP类输出创建一个用户对象,然后将其传递给sign_in帮助器--sign_in帮助器应该创建一个具有全局作用域的当前用户对象。您必须以某种方式将该信息保存在会话中,通过将其存储在数据库或cookies中。您如何存储用户成功登录的事实?我没有看到任何对会话的调用来存储此信息。由于我对rails的理解有限,它不是通过sessions\u controller.rb中的“登录用户”传递的吗?从LDAP类输出创建一个用户对象,然后将其传递给sign_in helper——sign_in helper应该创建一个具有全局作用域的当前用户对象。你必须以某种方式在会话中保存该信息,或者将其存储在DB或Cookie中。这家伙刚刚开始学习rails,所以我猜他正在学习。我不是Desive Though的粉丝,这家伙刚刚起步,所以我想他正在学习。不过我不是Desive的超级粉丝