zoukankan      html  css  js  c++  java
  • ASP.NET Core 实现用户登录验证的最低配置

    背景是在一个项目中增加临时登录功能,只需验证用户是否登录即可,所需的最低配置与实现代码如下。

    在 Startup 的 ConfigureServices() 方法中添加 Authentication 的配置:

    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    }).AddCookie();

    在 Startup 的 Configure() 方法中将 Authentication 添加到请求管线:

    app.UseAuthentication();

    在登录程序中验证通过用户名/密码后,通过下面的代码生成登录 Cookie 并发送给客户端:

    var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, model.Email) }, "Basic");
    var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
        claimsPrincipal);
  • 相关阅读:
    制作文件的备份
    文件的读写
    文件的打开与关闭
    文件操作介绍
    数据类型转换
    位运算
    进制
    函数使用注意事项
    匿名函数
     递归函数
  • 原文地址:https://www.cnblogs.com/dudu/p/7631927.html
Copyright © 2011-2022 走看看