思路::在startup.cs的configservice中配置自定义鉴权方式,在界面使用该方式
本章节实现一个简单的自定义鉴权:要求每个url后面要有参数UrlToken并且值为liping-123456,才可以访问成功,否则失败
1:配置一个自定义鉴权的文件
1 public class CustomerAuthenticationConfig 2 { 3 public static void ConfigureServices(IServiceCollection services) 4 { 5 6 ////手写一个鉴权 7 services.AddAuthentication(options => 8 { 9 options.AddScheme<UrlTokenAuthenticationHandler>( 10 UrlTokenAuthenticationDefaults.AuthenticationScheme, 11 "ss"); 12 13 options.DefaultAuthenticateScheme = UrlTokenAuthenticationDefaults.AuthenticationScheme; 14 options.DefaultForbidScheme = UrlTokenAuthenticationDefaults.AuthenticationScheme; 15 options.DefaultChallengeScheme = UrlTokenAuthenticationDefaults.AuthenticationScheme; 16 options.DefaultSignInScheme = UrlTokenAuthenticationDefaults.AuthenticationScheme; 17 options.DefaultSignOutScheme = UrlTokenAuthenticationDefaults.AuthenticationScheme; 18 }); 19 20 } 21 22 public static void Configure(IApplicationBuilder app, IWebHostEnvironment env) 23 { 24 25 app.UseAuthentication(); 26 } 27 }
2:具体如何实现鉴权的
1 public class UrlTokenAuthenticationHandler : IAuthenticationHandler 2 { 3 private HttpContext context = null; 4 private AuthenticationScheme scheme = null; 5 6 /// <summary> 7 /// 初始化 8 /// </summary> 9 /// <param name="scheme"></param> 10 /// <param name="context"></param> 11 /// <returns></returns> 12 public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context) 13 { 14 this.context = context; 15 this.scheme = scheme; 16 return Task.CompletedTask; 17 } 18 /// <summary> 19 /// 异步鉴权 20 /// 21 /// </summary> 22 /// <returns></returns> 23 public Task<AuthenticateResult> AuthenticateAsync() 24 { 25 var urlInfo = context.Request.Query["UrlToken"]; 26 27 if (string.IsNullOrEmpty(urlInfo)) 28 { 29 30 return Task.FromResult(AuthenticateResult.NoResult()); 31 } 32 else if ("liping-123456".Equals(urlInfo)) 33 { 34 35 var identity = new ClaimsIdentity(); 36 identity.AddClaim(new Claim(ClaimTypes.Name, "liping")); 37 identity.AddClaim(new Claim(ClaimTypes.Role, "Admin")); 38 identity.AddClaim(new Claim(ClaimTypes.Email, "[email protected]")); 39 identity.AddClaim(new Claim(ClaimTypes.Country, "China")); 40 41 42 ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(); 43 claimsPrincipal.AddIdentity(identity); 44 45 return Task.FromResult( 46 AuthenticateResult.Success( 47 new AuthenticationTicket(claimsPrincipal, null, this.scheme.Name) 48 )); 49 } 50 else 51 { 52 return Task.FromResult(AuthenticateResult.Fail("没有urltoken")); 53 54 } 55 56 } 57 58 /// <summary> 59 /// 未登录 60 /// </summary> 61 /// <param name="properties"></param> 62 /// <returns></returns> 63 /// <exception cref="System.NotImplementedException"></exception> 64 public Task ChallengeAsync(AuthenticationProperties properties) 65 { 66 this.context.Response.Redirect("/Auth/noLogin"); 67 return Task.CompletedTask; 68 } 69 70 /// <summary> 71 /// 无权限 72 /// </summary> 73 /// <param name="properties"></param> 74 /// <returns></returns> 75 /// <exception cref="System.NotImplementedException"></exception> 76 public Task ForbidAsync(AuthenticationProperties properties) 77 { 78 this.context.Response.StatusCode = 403; 79 return Task.CompletedTask; 80 } 81 82 } 83 84 public static class UrlTokenAuthenticationDefaults 85 { 86 87 public const string AuthenticationScheme = "LipingUrlTokenScheme"; 88 89 }
3:调用
1 public class AuthController : Controller 2 { 3 4 5 /// <summary> 6 /// 使用url方式存储用户信息 7 /// https://localhost:44397/Auth/urltoken?UrlToken=liping-123456 8 /// https://localhost:44397/Auth/urltoken 9 /// </summary> 10 /// <returns></returns> 11 public async Task<IActionResult> UrlToken() 12 { 13 14 var user = base.HttpContext.User; 15 16 //startup方法中开放自己手写的鉴权方法 17 //通过https://localhost:44397/Auth/urltoken?UrlToken=liping-123456 18 //可以拿到登录信息 19 var result = await base.HttpContext.AuthenticateAsync(UrlTokenAuthenticationDefaults.AuthenticationScheme); 20 21 22 if (result?.Principal == null) 23 { 24 return new JsonResult(new 25 { 26 Status = true, 27 MSg = "认证失败" 28 }); 29 } 30 else 31 { 32 List<string> infoList = new List<string>(); 33 foreach (var c in base.HttpContext.User.Identities.FirstOrDefault().Claims) 34 { 35 infoList.Add(c.Value); 36 37 } 38 var data = Newtonsoft.Json.JsonConvert.SerializeObject(infoList); 39 40 return new JsonResult(new 41 { 42 Status = true, 43 MSg = "成功", 44 Data = data 45 46 }); 47 } 48 49 } 50 51 52 53 public IActionResult NoLogin() 54 { 55 return View(); 56 } 57 }
标签:core,Task,return,AuthenticationScheme,url,public,new,鉴权 From: https://www.cnblogs.com/hanliping/p/18369758