Tuesday, December 23, 2008

Solution to JavaScript File Include Problem From ASP.Net MasterPage

You can include javascript files using a simple script tag. However, you will soon discover that, when you are using this master page from content pages that are at different levels in folder hierarchy then the script files will be missing at some cases. This is due to the fact that, script file paths are referenced relative to the content page and NOT to the master page. As a result, despite having the correct include wrt the master page, your content pages may still miss the scripts!

The solution is simple. Add this following line (of course, replacing the sample values with your own ones) to your master page's page load method to add the script tag through code instead of through markup.

this.Page.ClientScript.RegisterClientScriptInclude("YourKey", ResolveUrl("~/Scripts/myscript.js");

Hope it saves you from the pain of adding a common js file into each of your content pages.

Asp.Net Membership: How to change a user's password from an admin account without knowing the current password?

Like mine, you may also need to change a user's password from an admin account. By default, when you are using ASP.Net authentication and storing the password in the hashed format, you will not be able to see the existing password in its decrypted form. Also, to change a password using the Membership API, you will need to know the existing password. However, here is a simple solution to the problem -


The idea is, you can reset the password of a user using the following code-

string tempPass = Membership.Provider.ResetPassword("username", string.Empty)

Make sure you have the config that allows you to change password without an answer to the security question. Now, you can invoke the ChangePassword method using this tempPass as the existing pass and your custom password as the new password. Refer to the following line for a compact implementation-

Membership.Provider.ChangePassword("username", Membership.Provider.ResetPassword("username", string.Empty), "custompass")

I believe this will save your time with a similar need.