C# ASP.NET Core 2未配置任何身份验证处理程序来处理方案
我想在ASP.NET Core 2应用程序中提供授权。 在发送带有Account/Login中的数据的模型后,在调用C# ASP.NET Core 2未配置任何身份验证处理程序来处理方案,c#,asp.net,asp.net-core,authorization,C#,Asp.net,Asp.net Core,Authorization,我想在ASP.NET Core 2应用程序中提供授权。 在发送带有Account/Login中的数据的模型后,在调用Wait Authenticate(user)后,我收到一条错误消息。 我不明白哪里缺少描述 Startup.cs //ConfigureServices services.AddAuthentication(options => { options.DefaultChallengeScheme = CookieAuthenticationDefaults.Authe
Wait Authenticate(user)
后,我收到一条错误消息。
我不明白哪里缺少描述
Startup.cs
//ConfigureServices
services.AddAuthentication(options =>
{
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie("TmiginScheme", options =>
{
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
options.ExpireTimeSpan = TimeSpan.FromHours(1);
options.SlidingExpiration = true;
});
//Configure
app.UseAuthentication();
AccountController
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginModel model)
{
if (ModelState.IsValid)
{
User user = null;
Cryptex cryptex = new Cryptex();
string password = cryptex.EncryptText(model.Password, "TMigin");
// Ищем user
user = fStorage.Users.GetUserByLogin(model.Login);
if (user != null)
{
if (string.Compare(user.Password, password) != 0)
{
user = null;
}
}
if (user != null)
{
await Authenticate(user);
return RedirectToAction("Index", "CMS");
}
else
{
// Логируем ошибку входа
ModelState.AddModelError("", "Ошибка входа");
}
}
return View(model);
}
private async Task Authenticate(User user)
{
var claims = new List<Claim>
{
new Claim(ClaimsIdentity.DefaultNameClaimType, user.Name),
new Claim("CMS", "True")
};
var identity = new ClaimsIdentity(claims);
var principal = new ClaimsPrincipal(identity);
await HttpContext.Authentication.SignInAsync("TmiginScheme", principal);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务登录(LoginModel模型)
{
if(ModelState.IsValid)
{
User=null;
Cryptex-Cryptex=新的Cryptex();
字符串密码=cryptex.EncryptText(model.password,“TMigin”);
//用户
user=fStorage.Users.GetUserByLogin(model.Login);
如果(用户!=null)
{
if(string.Compare(user.Password,Password)!=0)
{
user=null;
}
}
如果(用户!=null)
{
等待认证(用户);
返回重定向操作(“索引”、“CMS”);
}
其他的
{
// Логируем ошибку входа
ModelState.AddModelError(“,”бббббббббббб1073;
}
}
返回视图(模型);
}
专用异步任务身份验证(用户)
{
var索赔=新列表
{
新索赔(ClaimsIdentity.DefaultNameClaimType,user.Name),
新索赔(“CMS”、“真实”)
};
var标识=新的索赔实体(索赔);
var principal=新的ClaimsPrincipal(身份);
等待HttpContext.Authentication.SignInAsync(“TmiginScheme”,主体);
}
固定的
不工作,因为我将代码放在app.UseMvc(…){}
之后。
在屏幕截图中选择正确的位置
我想问题在于,当您使用
options.DefaultAuthenticateScheme=CookieAuthenticationDefaults.AuthenticationScheme时,您正在将默认方案配置为Cookies
当您使用AddCookie(“TmiginScheme”
时,您使用了不同的方案,即TmiginScheme
而在AccountController
中,您创建了新的ClaimsIdentity
,但未指定身份验证类型,最后尝试使用与options.DefaultSignInScheme=CookieAuthenticationDefaults.AuthenticationScheme;
中指定的不同的方案名称登录
要解决您的问题,请将AddCookie(“TmiginScheme”
更改为.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme
)
将var identity=newclaimsidentity(索赔);
更改为var identity=newclaimsidentity(索赔,CookieAuthenticationDefaults.AuthenticationScheme);
最后将wait-HttpContext.Authentication.SignInAsync(“TmiginScheme”,principal);
更改为wait-HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,principal);
我也遇到了同样的问题,但作者的解决方案对我来说并不奏效。
我正在从.NETCore1.1迁移到.NETCore2.0
就我而言,我使用的是:
await HttpContext.Authentication.SignInAsync(...);
await HttpContext.Authentication.SignOutAsync(...);
我应该使用:
await HttpContext.SignInAsync(...);
await HttpContext.SignOutAsync(...);
你是个救命恩人。我已经试了整整8个多小时了,结果证明这就是解决办法。