.net Core 用户登入身份验证

首次发布:2019-09-14 12:16
2019-10-06 更新
下面是.net Core 配置信息添加
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace NetCoreDemo
{
    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)
        {//转载请保留原创地址 http://www.luofenming.com/show.aspx?id=ART2019091400001
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //路由设置默认起始为  指定的Hmoe/Center
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Center}");
            });
            //使用身份验证服务
            app.UseAuthentication();
        }
    }
}
以下是 控制器代码
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace NetCoreDemo.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("退出成功");
        }
    }
}