码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET Identity 2.1 Empty Web From 方式

时间:2017-12-02 16:16:20      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:block   .text   word   auth   sig   markdown   system   reg   public   

微软原文地址

  • 相关代码
    • 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<IdentityUser>();
                  var manager = new UserManager<IdentityUser>(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<IdentityUser>();
                  var userManager = new UserManager<IdentityUser>(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")
                  });
              }
          }
      }

ASP.NET Identity 2.1 Empty Web From 方式

标签:block   .text   word   auth   sig   markdown   system   reg   public   

原文地址:http://www.cnblogs.com/Three-Zjy/p/7954382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!