netcore权限控制_关于 DotNetCore 的自定义权限管理

本文介绍了如何在 .NET Core 中自定义权限控制,通过扩展 `Microsoft.AspNetCore.Authentication` 并实现 `IAuthenticationHandler` 等接口,创建自定义的身份验证处理器。在 `ConfigureServices` 中注册服务,并在 `Configure` 中使用权限检查。同时展示了登录、登出控制器方法以及使用 `[Authorize]` 特性进行角色权限检查。示例代码详细展示了整个流程,适合初学者参考。
摘要由CSDN通过智能技术生成

1、自定义权限需要扩展 Microsoft.AspNetCore.Authentication 实现一套接口

IAuthenticationHandler, IAuthenticationSignInHandler, IAuthenticationSignOutHandler

public classMyAuthenticationHandler : IAuthenticationHandler, IAuthenticationSignInHandler, IAuthenticationSignOutHandler

{public AuthenticationScheme Scheme { get; private set; }protected HttpContext Context { get; private set; }publicTask InitializeAsync(AuthenticationScheme scheme, HttpContext context)

{

Scheme=scheme;

Context=context;returnTask.CompletedTask;

}publicTask ChallengeAsync(AuthenticationProperties properties)

{

Context.Response.Redirect("/Account/login");returnTask.CompletedTask;

}public async TaskAuthenticateAsync()

{var result = await Task.Run(() =>{var cookie = Context.Request.Cookies["myCookie"];if (string.IsNullOrEmpty(cookie))

{returnAuthenticateResult.NoResult();

}return AuthenticateResult.Success(this.Deserialize(cookie));

});returnresult;

}publicTask ForbidAsync(AuthenticationProperties properties)

{

Context.Response.StatusCode= 403;returnTask.CompletedTask;

}publicTask SignInAsync(ClaimsPrincipal user, AuthenticationProperties properties)

{var ticket = newAuthenticationTicket(user, properties, Scheme.Name);

Context.Response.Cookies.Append("myCookie", Serialize(ticket));returnTask.CompletedTask;

}publicTask SignOutAsync(AuthenticationProperties properties)

{

Context.Response.Cookies.Delete("myCookie");returnTask.CompletedTask;

}private stringSerialize(AuthenticationTicket ticket)

{byte[] byteTicket =TicketSerializer.Default.Serialize(ticket);returnSystem.Text.Encoding.Default.GetString(byteTicket);

}private AuthenticationTicket Deserialize(stringticket)

{byte[] byteTicket =System.Text.Encoding.Default.GetBytes(ticket);returnTicketSerializer.Default.Deserialize(byteTicket);

}

}

2、在 ConfigureServices 中注册服务

//This method gets called by the runtime. Use this method to add services to the container.

public voidConfigureServices(IServiceCollection services)

{services.AddAuthentication(

option=>{

option.DefaultScheme= "myScheme";

option.AddScheme("myScheme", "demo scheme");

});

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.AddSession();

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

}

3、在 void Configure(IApplicationBuilder app, IHostingEnvironment env) 中使用权限检查

app.UseAuthentication();

4、在 Controller 中实现自己的 Login 、Logout

[AllowAnonymous]public async void Login(string username, stringpassword)

{var claimIdentity = new ClaimsIdentity("CustomApiKeyAuth");

claimIdentity.AddClaim(newClaim(ClaimTypes.Name, username));

claimIdentity.AddClaim(new Claim(ClaimTypes.Role, "Admin"));await HttpContext.SignInAsync("myScheme", newClaimsPrincipal(claimIdentity));await HttpContext.Response.WriteAsync($"Hello {username} login!");

}public async voidLogout()

{await HttpContext.SignOutAsync("myScheme");

}

5、在 Controller 中使用权限检查特性

[Authorize(Roles = "Admin")]public voidTest()

{var user =HttpContext.User;

HttpContext.Response.WriteAsync($"Test {user.Identity.Name}!");

}

6、测试

在浏览器上输入 https://localhost:44318/account/login?username="aaa"

系统输出: Hello "aaa" login!

在浏览器上输入 https://localhost:44318/account/test

系统输出 : Test "aaa"!

成功运行了。

7、结束语

虽然只是简单的框架代码,但实现了完整的流程控制。方便初学者。

需要源代码的朋友点这里下载。

8、参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值