Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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
C# Asp.Net核心-最简单的表单身份验证_C#_Asp.net Mvc_Asp.net Core_Forms Authentication - Fatal编程技术网

C# Asp.Net核心-最简单的表单身份验证

C# Asp.Net核心-最简单的表单身份验证,c#,asp.net-mvc,asp.net-core,forms-authentication,C#,Asp.net Mvc,Asp.net Core,Forms Authentication,我有一个旧的MVC5应用程序,它以最简单的形式使用表单身份验证。web.config中只存储了一个帐户,没有角色等 <authentication mode="Forms"> <forms loginUrl="~/Login/Index" timeout="30"> <credentials passwordFormat="Clear"> <user name="some-user" password="some-password

我有一个旧的MVC5应用程序,它以最简单的形式使用表单身份验证。web.config中只存储了一个帐户,没有角色等

<authentication mode="Forms">
  <forms loginUrl="~/Login/Index" timeout="30">
    <credentials passwordFormat="Clear">
      <user name="some-user" password="some-password" />
    </credentials>
  </forms>
</authentication>
就这样。 asp.net核心中是否有类似的东西(在简单性方面)?

并不是那么简单:)

  • 在Startup.cs中,配置方法

    app.UseCookieAuthentication(options =>
    {
      options.AutomaticAuthenticate = true;
      options.AutomaticChallenge = true;
      options.LoginPath = "/Home/Login";
    });
    
  • 添加Authorize属性以保护要保护的资源

    [Authorize]
    public IActionResult Index()
    {
      return View();
    }
    
  • 在Home Controller的Login Post action方法中,编写以下方法

    var username = Configuration["username"];
    var password = Configuration["password"];
    if (authUser.Username == username && authUser.Password == password)
    {
      var identity = new ClaimsIdentity(claims, 
          CookieAuthenticationDefaults.AuthenticationScheme);
    
      HttpContext.Authentication.SignInAsync(
        CookieAuthenticationDefaults.AuthenticationScheme,
        new ClaimsPrincipal(identity));
    
      return Redirect("~/Home/Index");
    }
    else
    {
      ModelState.AddModelError("","Login failed. Please check Username and/or password");
    }
    

  • 以下是github repo供您参考:

    要添加到Anuraj的答案中,有许多类已被弃用用于.Net Core 2。供参考:

    Startup.cs-在ConfigureServices中:

    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
        .AddCookie(o => o.LoginPath = new PathString("/account/login"));
    
    Startup.cs-在配置中:

    app.UseAuthentication();
    
    在您的帐户/登录控制器方法/无论您在何处进行身份验证:

    var claims = new[] { new Claim(ClaimTypes.Name, "MyUserNameOrID"),
        new Claim(ClaimTypes.Role, "SomeRoleName") };
    
    var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
    
    await context.SignInAsync(
        CookieAuthenticationDefaults.AuthenticationScheme, 
        new ClaimsPrincipal(identity));
    // Do your redirect here
    
    资料来源:


    FormAuth的身份验证与asp.net core不兼容。而是使用标识我使用您的代码,但它会给出一个错误
    无法将lambda表达式转换为Startup.cs中的类型
    ,configure method.@SanyamiVaidya您使用的是哪个版本的asp.net core?我使用的是1.0.1版本的asp.net core。请参阅,在使用jwt和cookies进行双重身份验证时,循环回登录表单时出现问题。原来是在等待上下文的变化;等待context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,new ClaimsPrincipal(identity));成功了。谢谢@AndyP9!
    var claims = new[] { new Claim(ClaimTypes.Name, "MyUserNameOrID"),
        new Claim(ClaimTypes.Role, "SomeRoleName") };
    
    var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
    
    await context.SignInAsync(
        CookieAuthenticationDefaults.AuthenticationScheme, 
        new ClaimsPrincipal(identity));
    // Do your redirect here