ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

.net core 认证与授权(一)

2020-02-06 16:05:45  阅读:264  来源: 互联网

标签:core Authenticate Name 证书 app 认证 new Home net


前言
.net core web并不是一个非常新的架构,很多文章提及到认证与授权这个过程,但是一般都会提及到里面的方法怎么用的,而不是模拟一个怎样的过程,所以我打算记录自己的理解。
什么是认证?我们大学毕业有学士证书和毕业证书,来证明你是一个学士。
什么是授权,比如说你被认证是我的朋友后,你可以拿着这个身份,可以进入我的朋友圈看动态。
那么.net core 的认证与授权是一个什么样的过程,在这里提出简单模式是我给你颁发了证书,证明了你的身份,然后呢,你可以拿到你的身份卡之后,你要经过验证,得到授权,然后进入中华人民共和国,就是这个过程。
正文部分均为我的理解,可能存在误差,如果不对请指正。

正文
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}

[Authorize]
public IActionResult Secret()
{
    return View();
}

public IActionResult Authenticate()
{
    return RedirectToAction("Index");
}

}
我有一个control,里面action有index ,Secret ,Authenticate。
index View:

Home Page

Secret View:

Secret

然后我们访问 https://localhost:44350/Home/Index 效果:

如我意料,完全ok的。
然后访问:
https://localhost:44350/Home/Secret
出现了错误:

给了非常详细的提示,没有详细的认证计划,没有默认的怀疑方案被找到。
我们看到,唯一和index 不同的是加入了Authorize这个属性标签,这是个授权的意思,但是提示给我们的是我们没有认证。
好的,说明吧授权之前要认证,要识别出身份,现实中也是这样,能证明你的只有你的身份证,没有身份证我怎么给你授权。
好的,那么就添加认证过程:
认证有非常多种,这里就以cookie来简单的介绍。

public void ConfigureServices(IServiceCollection services)
{
services.Configure(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});

services.AddAuthentication("CookieAuth").AddCookie("CookieAuth", config =>
{
    config.Cookie.Name = "Cook.Name";
    config.LoginPath = "/Home/Authenticate";
});

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

}
我添加了一段:

services.AddAuthentication(“CookieAuth”).AddCookie(“CookieAuth”, config =>
{
config.Cookie.Name = “Cook.Name”;
config.LoginPath = “/Home/Authenticate”;
});
如果没有cookie认证,那么就跳转到/Home/Authenticate 去认证。
在/Home/Authenticate 下面我没有做任何事情,仅仅是去跳转到/Home/Index
那么正常情况下,是会跳转到/Home/Index。
是的,当我们访问:
https://localhost:44350/Home/Secret
看到的效果是:
https://localhost:44350/Home/Index
这是如我们所料的,那么我们就在/Home/Authenticate做一些事情,比如说颁发证书。
好的,那我们就在/home/Authenticate 中颁发证书:

public IActionResult Authenticate()
{
var SchoolClaims = new List()
{
new Claim(ClaimTypes.Name,“Jack”),
new Claim(ClaimTypes.Email,“Jack@fmail.com”)
};

var LicensClaims = new List<Claim>()
{
    new Claim(ClaimTypes.Name,"Jack.li"),
    new Claim(ClaimTypes.Email,"Jack@fmail.com"),
    new Claim("begin","2000.10.1")
};
var SchoolIdentity = new ClaimsIdentity(SchoolClaims,"Student Identity");
var CarManagerIdentity = new ClaimsIdentity(LicensClaims, "Licens Identity");
var userPrincipal = new ClaimsPrincipal(new[] { SchoolIdentity, CarManagerIdentity });

HttpContext.SignInAsync(userPrincipal);
return RedirectToAction("Index");

}
看下代码顺序:
我们创建了List(),这些是什么呢?
就是我们的信息。
比如我们驾驶证上有我们的名字,编号。
然后通过:
new ClaimsIdentity(LicensClaims, “Licens Identity”);
生成了一个identity,也就是产生了一张证书,这种证书叫做:Licens Identity,当然我们随意名字。
var userPrincipal = new ClaimsPrincipal(new[] { SchoolIdentity, CarManagerIdentity });
我们一生中有身份证,学位证,驾驶证,这么多证书是不是需要一个管理的呢?ClaimsPrincipal就是用来管理证书的。
HttpContext.SignInAsync(userPrincipal); 就会产生证书并且输入到前台。

请看,对这个cooke.name 是不是特别熟悉呢?就是我们启用了cookie 认证,如果忘记了请往上看。
但是访问:
https://localhost:44350/Home/Secret
看到的效果还是:
https://localhost:44350/Home/Index
这是为啥呢?不是认证了吗?
其实:

services.AddAuthentication(“CookieAuth”).AddCookie(“CookieAuth”, config =>
{
config.Cookie.Name = “Cook.Name”;
config.LoginPath = “/Home/Authenticate”;
});
和:

public IActionResult Authenticate()
{
var SchoolClaims = new List()
{
new Claim(ClaimTypes.Name,“Jack”),
new Claim(ClaimTypes.Email,“Jack@fmail.com”)
};

var LicensClaims = new List<Claim>()
{
    new Claim(ClaimTypes.Name,"Jack.li"),
    new Claim(ClaimTypes.Email,"Jack@fmail.com"),
    new Claim("begin","2000.10.1")
};
var SchoolIdentity = new ClaimsIdentity(SchoolClaims,"Student Identity");
var CarManagerIdentity = new ClaimsIdentity(LicensClaims, "Licens Identity");
var userPrincipal = new ClaimsPrincipal(new[] { SchoolIdentity, CarManagerIdentity });

HttpContext.SignInAsync(userPrincipal);
return RedirectToAction("Index");

}
重新来看这两个的关系。
得到的错误是没有一个认证的方案,然后写了添加了cookie验证,然后下面的是去实现把证书装配到cookie中。
验证机制生效了,证书也到前台了。最大可能的可能就是没有去拿证书,或者说证书机制除了验证其他的步骤都没有,也就是没有启动证书验证这套流程。
需要加上在:

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
    app.UseAuthentication();
app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

}
启动身份认证:

app.UseAuthentication();

也就是我们说:

services.AddAuthentication(“CookieAuth”).AddCookie(“CookieAuth”, config =>
{
config.Cookie.Name = “Cook.Name”;
config.LoginPath = “/Home/Authenticate”;
});
是去验证证书,而app.UseAuthentication();是去拿证书,整理证书,没有拿到证书相当于没有证书,所以一直不通过。
然后在.net core 3.0中,分的更细,还有问我们是否启动授权。而2.0 app.UseAuthentication()就可以了。

总结
以上只是个人的理解,在后续介绍如何与数据库建立联系。
龙华大道1号http://www.kinghill.cn/LongHuaDaDao1Hao/index.html

ting2909 发布了0 篇原创文章 · 获赞 7 · 访问量 9588 私信 关注

标签:core,Authenticate,Name,证书,app,认证,new,Home,net
来源: https://blog.csdn.net/ting2909/article/details/104197372

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有