分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

ASP.NET Identity 2.1 Empty Web From 方式

发布时间:2023-09-06 01:28责任编辑:林大明关键词:.NETWeb

微软原文地址

  • 相关代码
    • 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 方式

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved