It is sometimes advised to store information in a separate file than to "hard code" them inside the program. This includes, for example, in SQL connections, the database name or the server name stored in a separate file, so you don't have to open the program project in Visual Studio just to change the configurations. This blog explains how to use an .INI file to store information there and retrieve that information inside your program.
1. Create an .INI file on notepad. Type the following on the file then save it as info.ini.
2. On your form, reference System.Runtime.InteropServices then add the following code:
In C#:
public static string strFilePath = Application.StartupPath + "\\info.ini";
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileStringW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, string lpReturnedString, int nSize, string lpFileName);
[DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileStringW")]
private static extern int WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFilename);
In VB .NET:
Dim strFilepath As String = My.Application.Info.DirectoryPath & "\info.ini"
Public Declare Unicode Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
3. Declare the following variables (global):
In C#:
In VB .NET:
4. Add the ReadIni() method:
In C#:
In VB .NET:
5. Show the contents to a message box.
Module Breadcrumbs
C#. VB. WinForms. ASP. Snippets. Basic Codes. Beginners' Guide.
Tuesday, August 5, 2014
Monday, August 4, 2014
Make An ASP Page Secure From Anonymous Users
This blogs shows how to make a page(s) secured from users who're not logged in from your website. This applies to either C# or VB.
1. Create a new folder named "pages" (or any name you desire). On this folder you will save the secured webpages.
2. Right click on the folder, click Add New Item, then choose Web Configuration File. Click Add to create the file.
3. Add the following code to your web config file:
4. Your pages inside this folder now should not be viewed by users not logged in.
1. Create a new folder named "pages" (or any name you desire). On this folder you will save the secured webpages.
2. Right click on the folder, click Add New Item, then choose Web Configuration File. Click Add to create the file.
3. Add the following code to your web config file:
4. Your pages inside this folder now should not be viewed by users not logged in.
Creating a Login Page in ASP .NET Using C#
This blog explains how to create a login page in ASP .Net using C#. The login uses MS SQL authentication.
1. From the Toolbox, drag a Login control from the Login tab to the aspx page.
2. Select the Login control. Go to Properties window, choose events, click on Authenticate, then press Enter. The window will redirect you to the C# coding page with the authenticate event.
3. Add the following code to Login1_Authenticate event.
*Login1 is the name of the Login control
*Login1.Username gets the text entered on the username textbox, same with the Login1.Password which gets the text entered on the password textbox.
4. Add the ValidateLogin method:
*This contains the query to read from your database, given that you have a Users table, username and password columns.
5. The whole code should look like this:
*Take note to reference System.Web and System.Data assemblies.
*The SqlConnection initializes your connection to your database.
6. On the Web.Config file, add the following:
*On the authentication tag, defaultUrl is the landing page after you logged in; loginUrl is the name of the login page.
*I added a defaultDocument because my demo was created from an empty website; I set the defaultDocument to my login page, which is Login.aspx.
1. From the Toolbox, drag a Login control from the Login tab to the aspx page.
2. Select the Login control. Go to Properties window, choose events, click on Authenticate, then press Enter. The window will redirect you to the C# coding page with the authenticate event.
3. Add the following code to Login1_Authenticate event.
*Login1 is the name of the Login control
*Login1.Username gets the text entered on the username textbox, same with the Login1.Password which gets the text entered on the password textbox.
4. Add the ValidateLogin method:
*This contains the query to read from your database, given that you have a Users table, username and password columns.
5. The whole code should look like this:
*Take note to reference System.Web and System.Data assemblies.
*The SqlConnection initializes your connection to your database.
6. On the Web.Config file, add the following:
*On the authentication tag, defaultUrl is the landing page after you logged in; loginUrl is the name of the login page.
*I added a defaultDocument because my demo was created from an empty website; I set the defaultDocument to my login page, which is Login.aspx.
Subscribe to:
Posts (Atom)