ASP.NET Identity ( Form Authentication ) 方法

 
ASP.NET Identity ( Form Authentication ) 方法
 

   Startup.cs
 
   using System;
   using System.Threading.Tasks;
   using Microsoft.Owin;
   using Owin;
   using Microsoft.AspNet.Identity;
   using Microsoft.Owin.Security.Cookies;
 
   [assembly: OwinStartup(typeof(IdentityWebForm.Startup))]
 
   namespace IdentityWebForm
   {
      public class Startup
      {
         public void Configuration(IAppBuilder app)
         {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
               AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
               LoginPath = new PathString("/Login")
            });
         }
      }
   }
 

 

   Registration.aspx
 
   <form id="form1" runat="server">
   <div>
      <h4 style="font-size: medium">Register a new user</h4>
      <hr />
      <p>
         <asp:Literal runat="server" ID="StatusMessage" />
      </p>
      <div style="margin-bottom:10px">
         <asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
         <div>
            <asp:TextBox runat="server" ID="UserName" />
         </div>
      </div>
      <div style="margin-bottom:10px">
         <asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
         <div>
            <asp:TextBox runat="server" ID="Password" TextMode="Password" />
         </div>
      </div>
      <div style="margin-bottom:10px">
         <asp:Label runat="server" AssociatedControlID="ConfirmPassword">Confirm password</asp:Label>
         <div>
            <asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" />
         </div>
      </div>
      <div>
         <div>
            <asp:Button runat="server" OnClick="CreateUser_Click" Text="Register" />
         </div>
      </div>
   </div>
   </form>
 

 

   Registration.aspx.cs
 
   using System;
   using System.Linq;
   using System.Web;
   using Microsoft.AspNet.Identity;
   using Microsoft.AspNet.Identity.EntityFramework;
   using Microsoft.Owin.Security;
 
   namespace IdentityWebForm
   {
      public partial class Registration : System.Web.UI.Page
      {
         protected void CreateUser_Click(object sender, EventArgs e)
         {
            UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
            UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
 
            IdentityUser user = new IdentityUser() { UserName = UserName.Text };
            IdentityResult result = manager.Create(user, Password.Text);
 
            if (result.Succeeded)
            {
               IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
               var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
 
               authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
               Response.Redirect("~/Login.aspx");
            }
            else
            {
               StatusMessage.Text = result.Errors.FirstOrDefault();
            }
         }
      }
   }
 

 

   Login.aspx
 
   <form id="form1" runat="server">
      <div>
 
         <h4 style="font-size: medium">Log In</h4>
         <hr />
 
         <asp:PlaceHolder runat="server" ID="LoginStatus" Visible="false">
            <p>
               <asp:Literal runat="server" ID="StatusText" />
            </p>
         </asp:PlaceHolder>
 
         <asp:PlaceHolder runat="server" ID="LoginForm" Visible="false">
            <div style="margin-bottom: 10px">
               <asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
               <div>
                  <asp:TextBox runat="server" ID="UserName" />
               </div>
            </div>
            <div style="margin-bottom: 10px">
               <asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
               <div>
                  <asp:TextBox runat="server" ID="Password" TextMode="Password" />
               </div>
            </div>
            <div style="margin-bottom: 10px">
               <div>
                  <asp:Button runat="server" OnClick="SignIn" Text="Log in" />
               </div>
            </div>
         </asp:PlaceHolder>
 
         <asp:PlaceHolder runat="server" ID="LogoutButton" Visible="false">
            <div>
               <div>
                  <asp:Button runat="server" OnClick="SignOut" Text="Log out" />
               </div>
            </div>
         </asp:PlaceHolder>
 
      </div>
   </form>
 

 

   Login.aspx.cs
 
   using System;
   using System.Web;
   using Microsoft.AspNet.Identity;
   using Microsoft.AspNet.Identity.EntityFramework;
   using Microsoft.Owin.Security;
 
   namespace IdentityWebForm
   {
      public partial class Login : System.Web.UI.Page
      {
 
         protected void Page_Load(object sender, EventArgs e)
         {
            if (!IsPostBack)
            {
               if (User.Identity.IsAuthenticated)
               {
                  StatusText.Text = string.Format("Hello {0}!!", User.Identity.GetUserName());
                  LoginStatus.Visible = true;
                  LogoutButton.Visible = true;
                  Response.Redirect("~/WebPage.aspx");
               } else {
                  LoginForm.Visible = true;
               }
            }
         }
 
         protected void SignIn(object sender, EventArgs e)
         {
            UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
            UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
            IdentityUser user = manager.Find(UserName.Text, Password.Text);
 
            if (user != null)
            {
               IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
               var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
 
               authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
               Response.Redirect("~/Login.aspx");
            } else {
               StatusText.Text = "Invalid username or password.";
               LoginStatus.Visible = true;
            }
         }
 
         protected void SignOut(object sender, EventArgs e)
         {
            IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
            authenticationManager.SignOut();
            Response.Redirect("~/Login.aspx");
         }
 
      }
   }
 

 

   WebPage.aspx
 
   <form id="form1" runat="server">
      <asp:Label ID="UserID" runat="server" Text="" />
   </form>
 

 

   WebPage.aspx.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.UI;
   using System.Web.UI.WebControls;
   using Microsoft.AspNet.Identity;
   using Microsoft.AspNet.Identity.EntityFramework;
 
   namespace IdentityWebForm
   {
      public partial class WebPage : System.Web.UI.Page
      {
         protected void Page_Load(object sender, EventArgs e)
         {
            if (!(User.Identity.IsAuthenticated))
            {
               Response.Redirect("~/Login.aspx");
            }
            else
            {
               this.UserID.Text = string.Format("Hello {0}!!", User.Identity.GetUserName());
            }
         }
      }
   }