ruby - 即使在 Sinatra 中运行/注销代码之后,Rack.session cookie 也不会被删除

标签 ruby authentication ldap sinatra session-cookies

我用 Ruby 开发了一个非常简单的 Sinatra 身份验证系统,当用户将应用程序的 URL 输入浏览器时,它会向用户显示一个浏览器表单。然后,他们输入登录详细信息,如果这些信息正确,则允许他们进入禁区。

即使我运行这个,我的问题是:-

        get '/logout' do

            session[:session_id] = nil
            current_user = false

                File.open(LOG_FILE, "a"){ |f| f.puts "Session closed... #{current_user}" } 


        end 

当我使用 Chrome 浏览器开发工具检查元素时,我仍然可以看到机架 session cookie。我希望 cookie 被删除,但它没有被删除。这样做的缺点是,当我输入应用程序的 url 时,我不再看到浏览器表单,因为 Sinatra 仍然觉得我仍然在已登录。

我已确保,无论我使用什么浏览器,都不会保存用户名和密码,但本质上我希望当我将/logout 路由放入浏览器时终止 session 并删除 cookie。

有人可以告诉我我需要做什么吗?我已经添加了身份验证方法和/路由的代码。

get '/' do 
            begin

                authenticate


                session[:username] = @auth.credentials[0]

                current_user = session[:username]
                File.open(LOG_FILE, "a"){ |f| f.puts " ***session created #{Time.now} #{current_user}" }

                erb :index
            rescue Exception => err
                File.open(LOG_FILE, "a"){ |f| f.puts " ***Daemon failure #{Time.now} err=#{err} " }
                File.open(LOG_FILE, "a"){ |f| f.puts " ***Daemon failure #{Time.now} err=#{err.backtrace.join("\n")} " }
            end

        end

验证.rb

定义验证 如果授权则返回true? headers['WWW-Authenticate'] = '基本领域='限制区域'' 暂停 401,“未授权\n” 结束

def authorized? 
    @auth ||=  Rack::Auth::Basic::Request.new(request.env)
    @auth.provided? and @auth.basic? and @auth.credentials and is_authenticated_by_ldap(@auth.credentials) 
end

def is_authenticated_by_ldap(凭证)

      EXTENSION = @praetor.qube 
      full_username = credentials[0] + EXTENSION

    ldap = Net::LDAP.new  :host => "just-ln1-wdc03.praetor.qube", # your LDAP host name or IP goes here,
                           :port => 389, # your LDAP host port goes here,
                      #:encryption => :simple_tls,
                           :base => "DC=praetor,DC=qube", # the base of your AD tree goes here,
                           :auth => 
                      {
                            :method => :simple,
                            #:username => credentials[0],
                            :username => full_username, # a user w/sufficient privileges to read from AD goes here,
                            :password => credentials[1] # the user's password goes here
                        }   

    is_authorized = ldap.bind


    return is_authorized
end

最佳答案

session.clear 将删除 session 变量。您仍然会在 Chrome 中看到一个 session ,因为它是在您的操作运行后由 Rack::Session::Cookie 中间件设置的,但这只是一个空白 session 。

关于ruby - 即使在 Sinatra 中运行/注销代码之后,Rack.session cookie 也不会被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27607996/

相关文章:

ruby - 具有 AES 计数器模式的随机数生成器 - Ruby

jquery 跨域认证

authentication - 管理 Facebook 身份验证 token

python - Tornado 登录示例/教程

azure - 支持 Active Directory Graph Api,无需 Azure 应用程序

https - 设置 TLS/LDAP 或 TLS/HTTP 连接后,线路上会发生什么情况?

ruby - 在 Ruby 中按方法调用方法

arrays - 使用数组键进行哈希到哈希的简单哈希

ruby - Ruby 中的并行性

c# - 查询过去 24 小时内创建的所有计算机对象的 LDAP