WEB API+MVC |我正在尝试制作一个简单的登录表单,但它不起作用



myconroller

[HttpPost]
public ActionResult Login(ProfileView modelo)
{

List<ProfileView> modelList = new List<ProfileView>();

HttpResponseMessage response = client.GetAsync(client.BaseAddress + "api/employee").Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<ProfileView>>(data);
foreach(var modelos in modelList)
{
if(modelos.Email == modelo.Email & modelos.Password == modelo.Password)
{

return View("Register");
}
}
}
return View("Login");

}

我的观点

@using (Html.BeginForm("Login", "Profile", FormMethod.Post))
{
@Html.TextBoxFor(m => m.Email)
@Html.TextBoxFor(m => m.Password)
<div class="form-group">
<div lass="container-login100-form-btn">
<input type="submit" value="Login" class="login100-form-btn" />
</div>
</div>
}

我不明白为什么我的控制器中的if语句变为false的部分,foreach中的if声明。

我使用的是这种使用ASP.NET Identity的简单登录方法,它可能对您有用。

[HttpGet]
public IActionResult Login()
{
return View();
}

[HttpPost]
public async Task<IActionResult> Login(LoginModel model)
{
if (ModelState.IsNotValid())
{
return View(model);
}
var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, true);
if (result.Succeeded)
return RedirectToAction("Index", "Blog");
if (result.IsLockedOut)
{
ModelState.AddModelError(string.Empty, "Account is locked out.");
return View(model);
}
ModelState.AddModelError(string.Empty, "Login Failed. UserName or Password is incorrect");
return View(model);
}

LoginViewModel为:

public class LoginModel
{
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
public bool RememberMe { get; set; }
}

_signInManager被注入构造函数中,如下所示:

private readonly SignInManager<User> _signInManager;
private readonly UserManager<User> _userManager;
public SecurityController(SignInManager<User> signInManager, UserManager<User> userManager)
{
_signInManager = signInManager;
_userManager = userManager;
}

startup.cs中,我也有这样的设置:

services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<ApplicationContext>()
.AddDefaultTokenProviders();

services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Security/Login";
options.LogoutPath = "/Security/Logout";
options.AccessDeniedPath = "/Security/AccessDenied";
options.SlidingExpiration = true;
options.Cookie = new CookieBuilder
{
HttpOnly = true,
Name = ".Freetime",
Path = "/",
SameSite = SameSiteMode.Lax,
SecurePolicy = CookieSecurePolicy.SameAsRequest
};
});

最新更新