# HTTP Basic Authentication

**URL:** <https://discuss.rubyonrails.org/t/http-basic-authentication/41812>\
**Category:** rubyonrails-talk\
**Created:** [April 22, 2009, 1:47pm UTC](https://discuss.rubyonrails.org/t/http-basic-authentication/41812 "2009-04-22T13:47:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hunt\_Jon](https://avatars.discourse-cdn.com/v4/letter/h/ea5d25/32.png) [@Hunt\_Jon](https://discuss.rubyonrails.org/u/Hunt_Jon)\
**Post date:** [April 22, 2009, 1:47pm UTC](https://discuss.rubyonrails.org/t/http-basic-authentication/41812/1 "2009-04-22T13:47:11Z")

</div>

I'm trying to HTTP Basic Authentication mentioned at

> **[\#82 HTTP Basic Authentication - RailsCasts](http://railscasts.com/episodes/82-http-basic-authentication)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

I'm trying to understand the document.

> <https://github.com/rails/rails/blob/be7b64b35aac1c9e9063d1d8317f8b1be2a3411c/actionpack/lib/action_controller/http_authentication.rb#L111>

The RailsCasts sample is:

def authenticate &nbsp;&nbsp;authenticate\_or\_request\_with\_http\_basic do |username, password| &nbsp;&nbsp;&nbsp;&nbsp;username == "foo" && password == "bar" &nbsp;&nbsp;end end

But I would like to change "realm" as well. It looks possible in the documentation. But I don't understand how to do it because my understanding of block in Ruby isn't good.

Can anybody give me a sample that uses "realm"?

John

---

<div class="post-metadata">

**Author:** ![Dmitry\_Sokurenko](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@Dmitry\_Sokurenko](https://discuss.rubyonrails.org/u/Dmitry_Sokurenko)\
**Post date:** [April 22, 2009, 1:49pm UTC](https://discuss.rubyonrails.org/t/http-basic-authentication/41812/2 "2009-04-22T13:49:20Z")

</div>

Try:

authenticate\_or\_request\_with\_http\_basic('YourRealm') do |username, password\> &nbsp;&nbsp;&nbsp;username == "foo" && password == "bar" end

Dmitry

---

<div class="post-metadata">

**Author:** ![Hunt\_Jon](https://avatars.discourse-cdn.com/v4/letter/h/ea5d25/32.png) [@Hunt\_Jon](https://discuss.rubyonrails.org/u/Hunt_Jon)\
**Post date:** [April 22, 2009, 1:51pm UTC](https://discuss.rubyonrails.org/t/http-basic-authentication/41812/3 "2009-04-22T13:51:19Z")

</div>

Thanks. That's how I can pass the first argument. Now I understand.

Thanks.

John

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [May 21, 2012, 2:01pm UTC](https://discuss.rubyonrails.org/t/http-basic-authentication/41812/4 "2012-05-21T14:01:11Z")

</div>

Does the code.....

http\_basic\_authenticate\_with :name=\>"foo" :password=\>"bar"

will this work?
