我阅读了this article,因此在我的 ASP.NET Core 5 应用程序中实现了
public void ConfigureServices(IServiceCollection services) {
// ...
services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();
// ...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
// ...
app.UseAuthentication();
app.UseAuthorization();
// ...
}
我有一个用户自定义类 - AppUser
和角色自定义类 - AppRole
。
我有一个获取当前用户的服务
public class UserResolverService : IUserResolverService
{
private readonly IHttpContextAccessor _context;
private readonly IRepository _repository;
public UserResolverService(IHttpContextAccessor context, IRepository repository)
{
_context = context;
_repository = repository;
}
public async System.Threading.Tasks.Task<AppUser> GetCurrentUserAsync()
{
var name = _context.HttpContext.User.Identity.Name.Split('\\')[1];
var users = await _repository.ListAsync<AppUser>();
var myUser = users.Where(u => u.UserName == name).FirstOrDefault();
if (myUser != null)
return myUser;
// if not found, create a new user;
DirectorySearcher ds = new DirectorySearcher
{
Filter = $"(&(objectCategory=person)(objectClass=user)(sAMAccountName={name}))"
};
SearchResult userProperty = ds.FindOne();
AppUser currentUser = new AppUser()
{
Email = userProperty.Properties["mail"][0].ToString(),
UserName = userProperty.Properties["sAMAccountName"][0].ToString(),
Last = userProperty.Properties["sn"][0].ToString(),
First = userProperty.Properties["givenName"][0].ToString()
};
return currentUser;
}
}
我应该如何将 AppUser
连接到它的 AppRole
?
我阅读了一些文章,但其中必须描述个人身份验证,而不是 Windows 身份验证......所以在这方面有点迷失。请解释基本授权使用哪些类和包的主要思想。
例如。 “用户”菜单条目不应对非管理员可见。我如何管理这部分?