Jiasaw 1.0.1

dotnet add package Jiasaw --version 1.0.1
NuGet\Install-Package Jiasaw -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Jiasaw" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Jiasaw --version 1.0.1
#r "nuget: Jiasaw, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Jiasaw as a Cake Addin
#addin nuget:?package=Jiasaw&version=1.0.1

// Install Jiasaw as a Cake Tool
#tool nuget:?package=Jiasaw&version=1.0.1

Jiasaw文档

导入包

PM>Install-Package Jiasaw -Version 1.0.0
或者Nuget管理界面搜索 Jiasaw

身份验证模块-Authentication

使用方法
  • 增加拦截器
   public static class WebApiConfig
   {
      public static void Register(HttpConfiguration config)
      {
        //增加拦截器
        config.Filters.Add(new AuthenticationFilter());            
      }
   }
  • Global.asax 设置身份验证拦截器全局参数
 public class WebApiApplication : System.Web.HttpApplication
 {
        protected void Application_Start()
        {
            //设置没有注解的Action是否需要验证授权
            //默认为true
            AuthenticationConfig.SetAuthenticateNoAttribute(false);
            //设置ticket是否在验证授权之后,每次请求都更新ticket过期时间
            //默认为false
            AuthenticationConfig.SetRefreshTicket(true);
            //设置ticket过期时间,单位为秒
            //默认为1小时过期
            AuthenticationConfig.SetTicketExpire(1000);
        }
 }
  • 需要授权的Action上增加注解
 [HttpGet]
 [Authentication(Authenticate =false)]
 public string Login()
 {
     object user = new object();
     List<string> roles = new List<string>() { "role1", "role2" };
     AuthenticationConfig.RegisterUserAndRole(user, roles);
     return "success";
 }
 

Authorization 值代表是否需要授权验证

AuthenticationConfig.RegisterUserAndRole(user, roles); 方法是存储当前用户信息,包括自定义的user,user对应的roles,roles值在验权值有用

  • 身份验证通过
ticket:ticket的值

身份验证通过时,接口返回头中有ticket,以及ticket对应的值

前端在请求需要身份验证的接口请求头中加上tikect以及对应的值

  • 身份验证失败
public class ExceptionFilter : IExceptionFilter
{
    public bool AllowMultiple =>true;

    public Task ExecuteExceptionFilterAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
    {
        if (actionExecutedContext.Exception is AuthenticationException)
        {
        	//log 
        }
    	return null;
    }
}

项目中定义全局异常捕获,身份验证失败时,会抛出AuthenticationException,后续操作自行处理

权限验证模块-Authorization

使用方法
  • 增加拦截器
   public static class WebApiConfig
   {
      public static void Register(HttpConfiguration config)
      {
        //增加拦截器
        config.Filters.Add(new AuthorizationFilter());            
      }
   }
  • Global.asax 设置身份验证拦截器全局参数
 public class WebApiApplication : System.Web.HttpApplication
 {
     protected void Application_Start()
     {
        //设置没有注解是否要验权
        //默认为true
     	AuthorizationConfig.SetAuthorizationNoAttribute(false);
     }
 }
  • 需要授权的Action上增加注解
 [HttpGet]
 [Authorization(Authorization =true,Logical =Logical.OR,Roles ="1,2,3,4")]
 public string Test()
 {     
     return "success";
 }
 

Authorization 值代表示否需要验证权限

Logical =Logical.OR|Logical.AND 表示 user roles 和 Authorization 的Roles验证逻辑

Logical.OR :user roles只要有Authorization 的Roles其中一个

Logical.AND:user roles必须有Authorization 的Roles所有值

Roles:角色配置,多个角色以 , 分隔

  • 身份验证失败
public class ExceptionFilter : IExceptionFilter
{
    public bool AllowMultiple =>true;

    public Task ExecuteExceptionFilterAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
    {
        if (actionExecutedContext.Exception is AuthorizationException)
        {
        	//log 
        }
    	return null;
    }
}

项目中定义全局异常捕获,身份验证失败时,会抛出AuthorizationException,后续操作自行处理

Product Compatible and additional computed target framework versions.
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 450 10/17/2019
1.0.0 441 10/17/2019

Summary of changes made in this release of the package.