随笔 - 432  文章 - 0  评论 - 15  阅读 - 63万

.net Core 用户登入身份验证简单的demo

下面是.net Core Startup文件的配置信息,关注标红的地方

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
  
namespace Demo
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            //添加 身份验证 服务
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).
             AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o =>
             {
                 o.LoginPath = new PathString("/Home/Login");
             });
        }
  
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
  
            //路由设置默认起始为  指定的Hmoe/Center
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Center}");
            });
            //使用身份验证服务
            app.UseAuthentication();
        }
    }
}

  以下是 控制器代码,关注标红的地方

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
  
namespace Demo.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Login()
        {
            return Content("Login");
        }
        public IActionResult DoLogin()
        {
            /*
             * 记录cookie之前要对用户的帐号和密码进行验证
             * 如果验证成功则把id和用户名记入 cookie
             * (帐号和密码验证要查询数据库 我在这里就没有去处理,下面默认是验证通过后的代码)
             * 登录以后获取token,
             * 获取传递的token,去用户信息
             *
             */
            string token = "123456";
            string name = "狼来了";
            ClaimsIdentity identity = new ClaimsIdentity("Forms");
  
            identity.AddClaim(new Claim(ClaimTypes.Sid, token));
            identity.AddClaim(new Claim(ClaimTypes.Name, name));
  
            ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity);
            HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal);
  
            return Content("登录成功!!");
        }
  
        /// <summary>
        /// 用户进入内容的之前 先去用户信息进行验证
        /// 如果验证不通过则进入 Home/Login 这个是在添加服务配置时添加的
        /// </summary>
        /// <returns></returns>
        [Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
        public IActionResult Center()
        {
             string sid= User.FindFirstValue(ClaimTypes.Sid);//获取ID
             string mane= User.FindFirstValue(ClaimTypes.Name);//获取用户名
             return Content("Center");
        }
        public IActionResult Logout()
        {
           HttpContext.SignOutAsync().Wait();//注销
           return Content("退出成功!!");
        }
    }
}

  So easy!!!!!!

posted on   狼来了  阅读(2993)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 易语言 —— 开山篇
· Trae初体验
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示