1
HttpContextBase
不包含Authentication
的定義,並沒有擴展方法Authentication
接受HttpContextBase
類型的第一個參數可以找到(是否缺少using指令或程序集引用...HttpContextBase」不包含用於定義‘在MVC認證’
我曾嘗試另一種方式,但它顯示上面的錯誤...... HttpContext.SignInAsync
。我使用VS 2017年
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DangNhap(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
//TODO : Do something to authenticate the user
if (model.Username == "Admin" && model.Password == "admin")
{
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
identity.AddClaim(new Claim(ClaimTypes.Name, model.Username));
await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(identity));
return RedirectToLocal(returnUrl);
//var result =
// await SignInManager.PasswordSignInAsync(model.Username,
// model.Password, true, shouldLockout: false);
//switch (result)
//{
// case SignInStatus.Success:
// return RedirectToLocal(returnUrl);
// case SignInStatus.Failure:
// default:
// ModelState.AddModelError("", "Invalid login attempt.");
// return View(model);
//}
}
else
{
ModelState.AddModelError("Errors", "Username và password sai, hãy đăng nhập lại");
return View(model);
}
}
在這行你有沒有錯誤? – CodeNotFound
await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity)); – KSon
檢查是否安裝並正確引用了'Microsoft.Owin.Security'&'Microsoft.Owin.Host.SystemWeb'軟件包(嘗試使用'System.Web.HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,new ClaimsPrincipal(identity)); '而不是在控制器動作中使用'HttpContext'屬性)。 –