Web.config
is the main settings and configuration file for an ASP.NET web application. The file is an XML document that defines configuration information regarding the web application. The web.config file contains information that control module loading, security configuration, session state configuration, and application language and compilation settings. Web.config files can also contain application specific items such as database connection strings.
The below method is used to access non-secure keys/values in the web.config don't use it to access secure sections as the ConnectionString for example, because the value will be rendered in the html of the page.
Example 1:
<
configuration
>
<
appSettings
>
<
add
key
="
var1"
value
="
SomeValue"
/
>
<
/appSettings
>
<
configuration
>
In this article, we will see how to read the configuration settings in the web.config using ‘javascript’.
Step 1
: Create a new ASP.NET website. Add a button control to the Default.aspx.
Step 2
: Right click the project > Add New Item > Web Configuration File
Add the following sample entry to the appSettings section in the web.config between the <configuration> tag as shown in the example 1:
<
add
key
="
var1"
value
="
SomeValue"
/
>
Step 3
: To read these entries using javascript, add the following script in the <head> tag of your Default.aspx page as shown below:
<
head
runat
="
server"
>
<
title
>
<
/title
>
<
script
type
="
text/javascript"
>
function ReadConfigurationSettings()
var k = '
<%
=ConfigurationManager.AppSettings[
"
var1"
].ToString() %>'
alert(k);
<
/script
>
<
/head
>
Step 4
: Call this function on a button click and display the values of the configuration settings:
<input type=
"
button"
value
=
"
Get"
onclick=
"
ReadConfigurationSettings();"
/>
You can update this code to read any section in the web.config.
Now Run the application and click the button. The value of the key "var1" in the appSettings will be displayed in the alert window.
When I tried this code in aspx page(vb.net) it is throwing "Identifier expected" error.But, the same code is working fine in aspx page(C#).
Do i need to change anything while declaring variable?
Please help me out!
Thanks
Priya
use var mfpauthen = '<%=ConfigurationManager.AppSettings("loginauthentication").ToString() %>';
instead of square[] bracket use ordinary bracket ()
It says "Read Configuration Settings of Web.config using Javascript", but you are just hard-coding the value.
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
Dear Xmen,
this is not a hard-coding, it is about accessing server side code using inline code in javascript code
It's all about the usage of <%= something %> tag which allows (in place of something) referring any server side, code behind variable.
Regards,
Jamil
It's called hard-coding. Client is not accessing server's web.config, but server code is accessing its web.config(in your example). Javascript is not doing any part which access the value, its just reading a string.
here is misunderstanding
Read Configuration Settings of Web.config
using
( and injecting in ) Javascript
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
Web01
2.8:2023-05-13:1