Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to get config key value in a js file. i am able to get the key value if set in the aspx page. But i am not able to get the key value if tried with in a javascript file.
<add key="Data" value="7061737323313233"/>


What I have tried:

Code is as below
Config file
<add key="Data" value="7061737323313233"/>

function LoginAES() {
    
    var clearpass = document.getElementById('txtPassword').value;
    var Data = '<%=System.Configuration.ConfigurationManager.AppSettings["Data"].ToString() %>';
    alert(Data);
    

}
Posted
Updated 10-Nov-21 22:28pm
v2

1 solution

Server-side code blocks (<% ... %>) only run in ASP.NET pages / controls. They do not execute in static files like Javascript (*.js) or CSS (*.css).

You'll need to keep the server-side code block in your page, and find some other means to pass it to your Javascript file.

For example:

YourPage.aspx:
ASP.NET
...
<body>
    <script>
    const appSettings = {
        Data: "<%$ AppSettings:Data %>"
    };
    </script>
    ...
YourScript.js:
JavaScript
function LoginAES)({
    const data = window.appSettings.Data;
    alert(data);
}

NB: Your method name makes me think you are trying to hash the user's password on the client before submitting it to the server. That is a terrible idea! Aside from the fact that you won't be able to salt the password correctly, you effectively negate any benefits of hashing the password. The hash value is now the plain-text password, and you are storing that in your database verbatim.

If you're worried about someone intercepting the form submission between your users and your server, then make sure you site is only available over HTTPS. That way, the system will encrypt the data in-transit, and you can forget about trying to hash the password on the client, and do it properly instead.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]
Troy Hunt: Here's Why Your Static Website Needs HTTPS[^]
 
Share this answer
 
Comments
Member 15421351 11-Nov-21 5:27am    
above code is not working. All i need is to pass config key value to a js file.
Richard Deeming 11-Nov-21 5:29am    
"Not working" is not a description of a problem.

Would you phone your garage, say "My car is not working", and then hang up?

If you can't describe the problem, then nobody can help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900