Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Asp.Net核心中的旧用户表进行自定义身份验证_Asp.net_Asp.net Core_Asp.net Identity_Asp.net Authorization - Fatal编程技术网

使用Asp.Net核心中的旧用户表进行自定义身份验证

使用Asp.Net核心中的旧用户表进行自定义身份验证,asp.net,asp.net-core,asp.net-identity,asp.net-authorization,Asp.net,Asp.net Core,Asp.net Identity,Asp.net Authorization,我需要对现有遗留用户表中的用户进行身份验证,但如果可能的话,我希望在身份和授权的基础上进行身份验证,以便可以使用这些特性和属性。我们的大多数用户已经从一个更老的本机应用程序登录,我们希望将其保留到我们的Asp.net核心Intranet Web应用程序中。我在某地和其他地方看到了一些关于这个问题的话题,但似乎没有一个适合我的具体需求 我该怎么做最好 我们的大多数用户已经从一个更老的本地登录 我们希望将其带到Asp.net核心Intranet的应用程序 网络应用 您可以使用OWIN cookie身

我需要对现有遗留用户表中的用户进行身份验证,但如果可能的话,我希望在身份和授权的基础上进行身份验证,以便可以使用这些特性和属性。我们的大多数用户已经从一个更老的本机应用程序登录,我们希望将其保留到我们的Asp.net核心Intranet Web应用程序中。我在某地和其他地方看到了一些关于这个问题的话题,但似乎没有一个适合我的具体需求

我该怎么做最好

我们的大多数用户已经从一个更老的本地登录 我们希望将其带到Asp.net核心Intranet的应用程序 网络应用

您可以使用OWIN cookie身份验证中间件。在ASP.NET内核中对中间件做了一些改动

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

在登录操作方法中,我们首先使用现有的身份验证机制进行身份验证。然后将用户信息和角色名称传递给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);
}

您不需要使用Identity:@CuongLe,在接受的答案示例中,_userService.GetByEmail将是一个自定义用户服务,我可以在其中查询自己的数据库,对吗?是的,没错---
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);
    }
}
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
    });
    ...
}