# Add basic authentication for specific page

**URL:** <https://community.f5.com/t/add-basic-authentication-for-specific-page/254>\
**Category:** Technical Forums\
**Tags:** irules, devops, security, ltm, application-delivery\
**Created:** [January 1, 2015, 1:12pm UTC](https://community.f5.com/t/add-basic-authentication-for-specific-page/254 "2015-01-01T13:12:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yossi\_100626](https://avatars.discourse-cdn.com/v4/letter/y/9de053/32.png) [@Yossi\_100626](https://community.f5.com/u/Yossi_100626)\
**Post date:** [January 1, 2015, 1:12pm UTC](https://community.f5.com/t/add-basic-authentication-for-specific-page/254/1 "2015-01-01T13:12:00Z")

</div>

Hi,

Will appreciate if anyone could explain what am I doing wrong. I would like to protect a single specific page with basic authentication.

when HTTP\_REQUEST { if {[string tolower [HTTP::uri]] contains “somepage.jsp”} { event disable all return } binary scan [md5 [HTTP::password]] H\* password if { [class lookup “[HTTP::username]” authorized\_users] equals $password } { log local0. “User [HTTP::username] has been authorized to access virtual server [virtual name]”

} else { if { [string length [HTTP::password]] != 0 } { log local0. “User [HTTP::username] has been denied access to virtual server [virtual name]” }

HTTP::respond 401 WWW-Authenticate “Basic realm=\“Secured Area\”” } }

---

<div class="post-metadata">

**Author:** ![nitass](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/nitass/32/2270_2.png) [@nitass](https://community.f5.com/u/nitass)\
**Post date:** [January 1, 2015, 1:51pm UTC](https://community.f5.com/t/add-basic-authentication-for-specific-page/254/2 "2015-01-01T13:51:13Z")

</div>

you know event disable will disable the event on that connection, don’t you? subsequent http requests on the connection will not trigger the irule.

---

<div class="post-metadata">

**Author:** ![Yossi\_100626](https://avatars.discourse-cdn.com/v4/letter/y/9de053/32.png) [@Yossi\_100626](https://community.f5.com/u/Yossi_100626)\
**Post date:** [January 1, 2015, 2:07pm UTC](https://community.f5.com/t/add-basic-authentication-for-specific-page/254/3 "2015-01-01T14:07:05Z")

</div>

Nitass, thanks for trying to help. My aim is for the authentication to kick in only if a specific page was requested. Do you have any suggestions on how to make this work?

I made a small correction at the first stage:

```tcl
when HTTP_REQUEST {
if {not [string tolower [HTTP::uri]] contains "somepage.jsp"} {
    event disable all
return
}

```

My aim if for the following part to be executed only if the first part page was found

```tcl
binary scan [md5 [HTTP::password]] H* password
if { [class lookup "[HTTP::username]" authorized_users] equals $password } {
log local0. "User [HTTP::username] has been authorized to access virtual server [virtual name]"
} else {
if { [string length [HTTP::password]] != 0 } {
log local0. "User [HTTP::username] has been denied access to virtual server [virtual name]"
} 
HTTP::respond 401 WWW-Authenticate "Basic realm=\"Secured Area\""
}
}

```

---

<div class="post-metadata">

**Author:** ![nitass](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/nitass/32/2270_2.png) [@nitass](https://community.f5.com/u/nitass)\
**Post date:** [January 1, 2015, 2:12pm UTC](https://community.f5.com/t/add-basic-authentication-for-specific-page/254/4 "2015-01-01T14:12:47Z")

</div>

My aim is for the authentication to kick in only if a specific page was requested.

do you really need the event disable command? can you use only the return command (i.e. remove the event disable command)?

---

<div class="post-metadata">

**Author:** ![Yossi\_100626](https://avatars.discourse-cdn.com/v4/letter/y/9de053/32.png) [@Yossi\_100626](https://community.f5.com/u/Yossi_100626)\
**Post date:** [January 1, 2015, 2:20pm UTC](https://community.f5.com/t/add-basic-authentication-for-specific-page/254/5 "2015-01-01T14:20:08Z")

</div>

OK. tried to remove it.I am getting (same as before): “Error code: ERR\_CONNECTION\_RESET” no matter what URL I am trying to access.

When removing the iRule the site works as expected.

---

<div class="post-metadata">

**Author:** ![nitass](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/nitass/32/2270_2.png) [@nitass](https://community.f5.com/u/nitass)\
**Post date:** [January 1, 2015, 2:23pm UTC](https://community.f5.com/t/add-basic-authentication-for-specific-page/254/6 "2015-01-01T14:23:40Z")

</div>

tried to remove it.I am getting (same as before): “Error code: ERR\_CONNECTION\_RESET” no matter what URL I am trying to access.

have you checked /var/log/ltm? was there any error?

---

<div class="post-metadata">

**Author:** ![Yossi\_100626](https://avatars.discourse-cdn.com/v4/letter/y/9de053/32.png) [@Yossi\_100626](https://community.f5.com/u/Yossi_100626)\
**Post date:** [January 1, 2015, 3:10pm UTC](https://community.f5.com/t/add-basic-authentication-for-specific-page/254/7 "2015-01-01T15:10:26Z")

</div>

I see the following error message. Does this means I can not use NOT statement with the URI value?

```tcl
Jan 1 16:00:01 ltm2 err tmm1[17789]: 01220001:3: TCL error: /Common/LimitConnection
 - can't use non-numeric string as operand of "!" while executing "if {not [string tolower [HTTP::uri]] contains "somepage.jsp"}
{ return }"

```

---

<div class="post-metadata">

**Author:** ![nitass](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/nitass/32/2270_2.png) [@nitass](https://community.f5.com/u/nitass)\
**Post date:** [January 1, 2015, 3:17pm UTC](https://community.f5.com/t/add-basic-authentication-for-specific-page/254/8 "2015-01-01T15:17:38Z")

</div>

can you enclose it with parentheses?

e.g.

```tcl
not ([string tolower [HTTP::uri]] contains "somepage.jsp")

```

---

<div class="post-metadata">

**Author:** ![Yossi\_100626](https://avatars.discourse-cdn.com/v4/letter/y/9de053/32.png) [@Yossi\_100626](https://community.f5.com/u/Yossi_100626)\
**Post date:** [January 1, 2015, 3:28pm UTC](https://community.f5.com/t/add-basic-authentication-for-specific-page/254/9 "2015-01-01T15:28:00Z")

</div>

Is this OK?

```tcl
Jan 1 16:00:01 ltm2 err tmm[17788]: 01220001:3: TCL error: /Common/LimitConnect ion - can't use non-numeric string as operand of "!" while executing "if {not [string tolower [HTTP::uri]] contains "somepage.jsp"} { return }"

```

---

<div class="post-metadata">

**Author:** ![nitass](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/nitass/32/2270_2.png) [@nitass](https://community.f5.com/u/nitass)\
**Post date:** [January 1, 2015, 3:30pm UTC](https://community.f5.com/t/add-basic-authentication-for-specific-page/254/10 "2015-01-01T15:30:25Z")

</div>

sorry to confuse. i mean the irule. 🙂 e.g. if {not ([string tolower [HTTP::uri]] contains “somepage.jsp”)} { … }

---

<div class="post-metadata">

**Author:** ![Yossi\_100626](https://avatars.discourse-cdn.com/v4/letter/y/9de053/32.png) [@Yossi\_100626](https://community.f5.com/u/Yossi_100626)\
**Post date:** [January 4, 2015, 8:13am UTC](https://community.f5.com/t/add-basic-authentication-for-specific-page/254/11 "2015-01-04T08:13:21Z")

</div>

Hi Nitass,

Thank you, it worked!:

```tcl
`when HTTP_REQUEST {
if {not ([string tolower [HTTP::uri]] contains "somepage.jsp")} {
return
}
binary scan [md5 [HTTP::password]] H* password
if { [class lookup "[HTTP::username]" authorized_users] equals $password } {
log local0. "User [HTTP::username] has been authorized to access virtual server [virtual 
name]"

} else {
if { [string length [HTTP::password]] != 0 } {
log local0. "User [HTTP::username] has been denied access to virtual server [virtual name]"
} 
HTTP::respond 401 WWW-Authenticate "Basic realm=\"Secured Area\""
}
}`

```
