ASP.NET Identity 2.1 Empty Web From 方式

微软原文地址

  • 相关代码
    • 1.创建用户

      using Microsoft.AspNet.Identity;
      using Microsoft.AspNet.Identity.EntityFramework;
      using Microsoft.Owin.Security;
      using System;
      using System.Linq;
      using System.Web;

      namespace WebFormsIdentity
      {
      public partial class Register : System.Web.UI.Page
      {
      protected void CreateUser_Click(object sender, EventArgs e)
      {
      //Default UserStore constructor uses the default connection string named: DefaultConnection
      var userStore = new UserStore();
      var manager = new UserManager(userStore);
      var user = new IdentityUser() { UserName = UserName.Text };

      IdentityResult result = manager.Create(user, Password.Text);

      if (result.Succeeded)
      {
      var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
      var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
      authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
      Response.Redirect("~/Login.aspx");
      }
      else
      {
      StatusMessage.Text = result.Errors.FirstOrDefault();
      }
      }
      }
      }

    • 2.登陆

      using Microsoft.AspNet.Identity;
      using Microsoft.AspNet.Identity.EntityFramework;
      using Microsoft.Owin.Security;
      using System;
      using System.Web;
      using System.Web.UI.WebControls;

      namespace WebFormsIdentity
      {
      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;
      }
      else
      {
      LoginForm.Visible = true;
      }
      }
      }

      protected void SignIn(object sender, EventArgs e)
      {
      var userStore = new UserStore();
      var userManager = new UserManager(userStore);
      var user = userManager.Find(UserName.Text, Password.Text);

      if (user != null)
      {
      var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
      var userIdentity = userManager.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)
      {
      var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
      authenticationManager.SignOut();
      Response.Redirect("~/Login.aspx");
      }
      }
      }

    • 3.为OWIN Authentication(认证)配置应用程序

      using Microsoft.AspNet.Identity;
      using Microsoft.Owin;
      using Microsoft.Owin.Security.Cookies;
      using Owin;

      [assembly: OwinStartup(typeof(WebFormsIdentity.Startup))]

      namespace WebFormsIdentity
      {
      public class Startup
      {
      public void Configuration(IAppBuilder app)
      {
      // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
      app.UseCookieAuthentication(new CookieAuthenticationOptions
      {
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
      LoginPath = new PathString("/Login")
      });
      }
      }
      }

posted @ 2017-12-02 15:29  芝华塔尼欧  阅读(182)  评论(0)    收藏  举报