亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

asp.net core MVC之實(shí)現(xiàn)基于token的認(rèn)證

 更新時(shí)間:2021年05月07日 14:08:03   作者:CN_Simo  
這篇文章主要介紹了asp.net core MVC之實(shí)現(xiàn)基于token的認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

安裝Nuget包

項(xiàng)目中添加包:dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer

添加認(rèn)證配置

Startup類中添加如下配置:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddAuthentication(defaultScheme: JwtBearerDefaults.AuthenticationScheme);
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...
    app.UseAuthentication();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    }); 
}

AddAuthentication方法會(huì)向依賴注入容器添加認(rèn)證服務(wù)和它所使用的其他服務(wù),其參數(shù)defaultScheme用于指定當(dāng)未指定具體的認(rèn)證方案時(shí)將會(huì)使用的默認(rèn)方案,上例為Bearer認(rèn)證。

AddAuthentication方法的另一重載能夠使用AuthenticationOptions類為認(rèn)證過程中的每一個(gè)動(dòng)作指明所使用的認(rèn)證方案,如DefaultAuthenticateScheme、
DefaultChallengeScheme、
DefaultSignInScheme、
DefaultSignOutScheme、
DefaultForbidScheme。
如果沒有為這些屬性設(shè)置認(rèn)證方案,則將使用DefaultScheme屬性所指定的值。

當(dāng)添加JwtBearer認(rèn)證方式時(shí),JwtBearerOptions對(duì)象能夠配置該認(rèn)證的選項(xiàng),它的TokenValidationParameters屬性用于指定驗(yàn)證Token時(shí)的規(guī)則:

var tokenSection = Configuration.GetSection("Security:Token");
services.AddAuthentication(options => {
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options => {
    options.TokenValidationParameters = new TokenValidationParameters{
        ValidateAudience = true,
        ValidateLifetime = true,
        ValidateIssuer = true,
        ValidateIssuerSigningKey = true,
        ValidIssuer = tokenSection["Issuer"],
        ValidAudience = tokenSection["Audience"],
        IssuerSigningKey = new SymmetricSecurityKey(
            Encoding.UTF8.GetBytes(tokenSection["Key"])
        ),
        ClockSkew = TimeSpan.Zero
    };
});

TokenValidationParameters類作為Token驗(yàn)證參數(shù)類,它包含了一些屬性,這些屬性如ValidateAudience、ValidateIssuer、ValidateLifetime和ValidateIssuerSigningKey,它們都是布爾類型,用于指明是否驗(yàn)證相應(yīng)的項(xiàng);而ValidIssuer和ValidAudience屬性則用于指明合法的簽發(fā)者(Issuer)與接受方(Audience)。在上例中,它們的值都從配置文件中獲取;IssuerSigningKey屬性的值用于指定進(jìn)行簽名驗(yàn)證的安全密鑰,它的值為SymmetricSecurityKey對(duì)象,即對(duì)稱加密密鑰;ClockSkew屬性的值表示驗(yàn)證時(shí)間的時(shí)間偏移值。

上述代碼會(huì)從配置文件中讀取關(guān)于Token的信息,因此還需在appsettings.json中添加如下內(nèi)容。

"Security": {
  "Token": {
    "Issuer": "demo_issuer",
    "Audience": "demo_audience",
    "Key": "<your_secret_key>"
  }
}

為Controller添加認(rèn)證

接下來,為了使用ASP.NET Core的認(rèn)證功能來保護(hù)資源,應(yīng)為Controller或Action添加[Authorize]特性,該特性能夠?qū)崿F(xiàn)在訪問相應(yīng)的Controller或Action時(shí),要求請(qǐng)求方提供指定的認(rèn)證方式,它位于Microsoft.AspNetCore.Authorization命名空間中。需要為AuthorController和BookController添加該特性。

[Authorize]
public class AuthorController : ControllerBase
{
}
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class BookController : ControllerBase
{
}

如果使用了多個(gè)認(rèn)證方式,則可以使用[Authorize]特性的AuthenticationSchemes屬性指明當(dāng)前Controller或Action要使用哪一種認(rèn)證方式(如上例中的BookController);如果不設(shè)置,則會(huì)使用所添加認(rèn)證時(shí)設(shè)置的默認(rèn)方案;如果沒有設(shè)置默認(rèn)方案,則會(huì)出現(xiàn)InvalidOperationException異常,并提示未指定默認(rèn)方案;此外,如果為AuthenticationSchemes屬性指定了不存在的方案名稱,也會(huì)出現(xiàn)InvalidOperationException異常。

此時(shí)再訪問Book和Author資源,會(huì)出現(xiàn)401 Unauthorized異常:

如果要允許某個(gè)Action可以被匿名訪問,可以在Action方法上添加屬性標(biāo)記 [AllowAnonymous]:

[AllowAnonymous]
public async Task<ActionResult<IEnumerable<AuthorDto>>> GetAuthorsAsync([FromQuery] AuthorResourceParameters parameters)

添加認(rèn)證信息生成接口

JwtBearer中間件提供了對(duì)JWT的驗(yàn)證功能,然而并未提供生成Token的功能。要生成Token,可以使用JwtSecurityTokenHandler類,它位于System.IdentityModel.Tokens.Jwt命名空間,它不僅能夠生成JWT,由于它實(shí)現(xiàn)了ISecurityTokenValidator接口,因此對(duì)JWT的驗(yàn)證也是由它完成的。接下來,我們將創(chuàng)建一個(gè)Controller,它將會(huì)根據(jù)用戶的認(rèn)證信息生成JWT,并返回給客戶端。

在Controllers文件夾中創(chuàng)建一個(gè)Controller,名為AuthenticateController,內(nèi)容如下:

using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;

namespace Library.Api.Controllers
{
    [ApiController, Route("api/auth")]
    public class AuthenticateController : ControllerBase
    {
        public IConfiguration Configuration { get; }
        public AuthenticateController(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        [HttpPost("token", Name = nameof(GenerateToken))]
        public IActionResult GenerateToken(string username, string password)
        {
            if (username != "demouser" || password != "demopassword")
            {
                return Unauthorized();
            }

            var claims = new List<Claim>{
                new Claim(JwtRegisteredClaimNames.Sub, username)
            };

            var tokenConfigSection = Configuration.GetSection("Security:Token");
            var key = new SymmetricSecurityKey(
                Encoding.UTF8.GetBytes(tokenConfigSection["Key"])
            );
            var signCredential = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var jwtToken = new JwtSecurityToken(
                issuer: tokenConfigSection["Issuer"],
                audience: tokenConfigSection["Audience"],
                claims: claims,
                expires: DateTime.Now.AddMinutes(3),
                signingCredentials: signCredential
            );

            return Ok(new {
                token = new JwtSecurityTokenHandler().WriteToken(jwtToken),
                expiration = TimeZoneInfo.ConvertTimeFromUtc(jwtToken.ValidTo, TimeZoneInfo.Local)
            });
        }
    }
}

在AuthenticateController中的GenerateToken方法中,通過創(chuàng)建JwtSecurityToken對(duì)象,并使用JwtSecurityTokenHandler對(duì)象的WriteToken方法最終得到生成的JWT。當(dāng)創(chuàng)建JwtSecurityToken對(duì)象時(shí),我們可以指定issuer、audience以及當(dāng)前用戶的Claim信息,此外,還可以指定該Token的有效時(shí)間。這里需要注意,由于JWT不支持銷毀以及撤回功能,因此在設(shè)置它的有效時(shí)間時(shí),應(yīng)設(shè)置一個(gè)較短的時(shí)間(如上例中的3分鐘),這樣可以有效避免Token在意外被竊取后所帶來的風(fēng)險(xiǎn)。

現(xiàn)在就可以請(qǐng)求認(rèn)證接口獲取 token:

這時(shí)重新請(qǐng)求資源接口,在請(qǐng)求頭中添加Authorization項(xiàng),值為Bearer ,就可以得到結(jié)果了:

這次示例中,使用了固定的用戶名和密碼,實(shí)際情況中,用戶名和密碼通常是存在數(shù)據(jù)庫(kù)中的,可以使用ASP.NET Core Identity來實(shí)現(xiàn)這一功能。

到此這篇關(guān)于asp.net core MVC之實(shí)現(xiàn)基于token的認(rèn)證的文章就介紹到這了,更多相關(guān)asp.net core token的認(rèn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論