我正在为 Rails 应用程序开发基本身份验证系统。身份验证使用 net-ldap 类验证来自 Active Directory 的帐户信息(这部分工作正常)。
然而,我的 session_helper 似乎有问题。即使 ActiveDirectoryUser.authenticate 成功,signed_in 帮助程序总是返回 false。登录后,脚本重定向到 root_path(default_controller 的主目录),然后立即再次重定向回 signin_path——因为 signed_in 助手返回 false。
请参阅下面的代码。我错过了什么?
谢谢
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
end
default_controller.rb
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
session_helper.rb
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
session_controller.rb
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