我需要从现有的旧用户表中对用户进行身份验证,但如果可能的话,我想在身份和授权的肩膀上进行,以便我可以使用功能和属性。我们的大多数用户已经从一个更旧的 native 应用程序登录,我们希望将其转移到我们的 Asp.net Core Intranet Web 应用程序。我在 SO 和其他地方看到了一些关于这个问题的主题,但似乎没有一个适合我的特定需求。

我应该做的最好的方法是什么?

最佳答案



您可以使用 OWIN cookie 身份验证中间件。中间件在 ASP.NET Core 中略有变化。

仅供引用 :Microsoft 正在转向基于策略的授权而不是基于角色的授权。角色声明是为了向后兼容。如果您计划将 AuthorizeAttribute 与角色一起使用,您可能需要查看此 sampleusage

https://leastprivilege.com/2016/08/21/why-does-my-authorize-attribute-not-work/

Login Action Method

在 Login 操作方法中,我们首先使用现有的身份验证机制进行身份验证。然后将用户信息和角色名称传递给 SignManager 方法。

[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl)
{
    ViewData["ReturnUrl"] = returnUrl;
    if (ModelState.IsValid)
    {
        bool result = // Validate user
        if (result)
        {
            var user = await _userRepository.GetUserByUserNameAsync(model.UserName);
            var roleNames = (await _roleRepository.GetRolesForUser(user.Id))
                 .Select(r => r.Name).ToList();
            await _signInManager.SignInAsync(user, roleNames);
        }
        else
        {
            ModelState.AddModelError("", "Incorrect username or password.");
        }
    }
    return View("Login", model);
}

SigIn Manager

然后我们创建用户信息作为声明。
public class SignInManager
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public SignInManager(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public async Task SignInAsync(User user, IList<string> roleNames)
    {
        var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Sid, user.Id.ToString()),
                new Claim(ClaimTypes.Name, user.UserName),
                new Claim(ClaimTypes.GivenName, user.FirstName),
                new Claim(ClaimTypes.Surname, user.LastName)
            };

        foreach (string roleName in roleNames)
        {
            claims.Add(new Claim(ClaimTypes.Role, roleName));
        }

        var identity = new ClaimsIdentity(claims, "local", "name", "role");
        var principal = new ClaimsPrincipal(identity);

        await _httpContextAccessor.HttpContext.Authentication
             .SignInAsync(Constants.AuthenticationScheme, principal);
    }

    public async Task SignOutAsync()
    {
        await _httpContextAccessor.HttpContext.Authentication
            .SignOutAsync(Constants.AuthenticationScheme);
    }
}

Startup.cs

最后但并非最不重要的一点是,我们然后在 Startup.cs 中添加 cookie 身份验证中间件。
public void Configure(IApplicationBuilder app,
       IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        Events = new CookieAuthenticationEvents
        {
            OnRedirectToAccessDenied = context =>
            {
                context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                return TaskCache.CompletedTask;
            }
        },
        AuthenticationScheme = Constants.AuthenticationScheme,
        LoginPath = new PathString("/Account/Login"),
        AccessDeniedPath = new PathString("/Common/AccessDenied"),
        AutomaticAuthenticate = true,
        AutomaticChallenge = true
    });
    ...
}

关于asp.net - 在 Asp.Net Core 中使用旧用户表进行自定义身份验证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45696376/

10-12 12:49
查看更多