In this article we will introduce with Login using web.Config file in ASP.NET using C#
Login using web.Config file in ASP.NET using C#
In this example we use the Form Authentication for Login. You will store the username and password in Web.Config file. You can see the following code.
Web.Config file:
<authentication mode="Forms"> <forms loginUrl="LoginWebConfig.aspx" defaultUrl="default2.aspx" name="myc" timeout="20" protection="All" path="/" requireSSL="false" slidingExpiration="true" cookieless="UseDeviceProfile" enableCrossAppRedirects="false"> <credentials passwordFormat="Clear"> <user name="Bikrantsingh" password="bikrant123"/> <user name="Anuragsingh" password="anurag"/> </credentials> </forms> </authentication> <authorization> <allow users="admin" /> <deny users="2"/> </authorization>
LoginWebConfig.aspx (Design Page):
LoginWebConfig.aspx (source code):
View Source code Click Here
LoginWebConfig.aspx.cs (C# code file):
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Security; public partial class LoginWebConfig : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void login_Button_Click(object sender, EventArgs e) { if (FormsAuthentication.Authenticate(username_Txt.Text, password_Txt.Text)) { string username = username_Txt.Text; FormsAuthentication.Initialize(); String strRole = username_Txt.Text; FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, username_Txt.Text, DateTime.Now, DateTime.Now.AddMinutes(30), false, strRole, FormsAuthentication.FormsCookiePath); Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat))); Session["UserAuthentication"] = username; Response.Redirect(FormsAuthentication.GetRedirectUrl(username_Txt.Text, false)); } else { Label1.Text = "Invalid Email ID and Password"; } } }
Output:
Run the application and fill username and password and click login button.
When you click on Login button the page will redirect on other web page.
Download source code
[ 0 ] Comments