26 Aralık 2012 Çarşamba

easy login mechanism for demo purposes

When you need to prepare a quick demo for a few users and you have not implemented your login mechanism yet, you can easily add forms authentication to you asp.net web application.

a simple login form (.aspx file)
<p>
        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label>
        <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
    </p>
    <p>
        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
        <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
    </p>

server side (aspx.cs file):
         protected void Page_Load(object sender, EventArgs e)
        {
            if (FormsAuthentication.Authenticate(this.UserName.Text, this.Password.Text))
            {
                FormsAuthentication.RedirectFromLoginPage(this.UserName.Text, true);
            }
        }

add the following lines to web.config file:
     <authentication mode="Forms">
      <forms loginUrl="~/menu/Login.aspx"  defaultUrl="~/menu/MenuMyApp.aspx" timeout="2880" cookieless="AutoDetect">
        <credentials passwordFormat="Clear">
          <user name="demo1" password="1"/>
          <user name="demo2" password="2"/>
        </credentials>
      </forms>
    </authentication>
    <authorization>
      <deny users="?"/>
    </authorization>

Your demo app is secure now.. Demo users can only login with the credentials specified.



Hiç yorum yok:

Yorum Gönder