【发布时间】:2018-09-23 10:59:08
【问题描述】:
我正在与IdentityServer4、Asp.Net.Identity 合作开展一个项目,所有工作都在MVC 和Razor 中完成。我的大部分工作都在工作,唯一让我苦苦挣扎的是 RememberMe 功能。目前 Idsrv 会设置 cookie,即使在登录时 RememberMe 为 false。
这是我的代码:
LoginModel 使用 oidc 身份验证方案挑战 Idsrv。
public class LoginModel : PageModel {
public IActionResult OnGet() {
return Challenge(new AuthenticationProperties {
RedirectUri = "/" }, "oidc");
}
}
}
这是我的客户端认证启动扩展方法
public static void AddClientAuthentication(this IServiceCollection services) {
services.AddAuthentication(options => {
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options => {
options.SignInScheme = "Cookies";
options.Authority = AuthorityUrl;
options.RequireHttpsMetadata = true;
options.ResponseType = "code id_token";
options.SaveTokens = true;
options.ClientId = "*ClientId*";
options.ClientSecret = "*ClientSecret*";
options.Scope.Add("profile");
options.Scope.Add("openid");
});
}
这是我的登录逻辑:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model) {
if (!ModelState.IsValid) {
return View(model);
}
SignInResult result = await signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberLogin, true);
if (result.Succeeded) {
ApplicationUser user = await userManager.FindByEmailAsync(model.Email);
await events.RaiseAsync(new UserLoginSuccessEvent(user.UserName, user.Id, user.UserName));
return Redirect("https://localhost:5002/home");
}
问题是,即使model.RememberLogin 为假,cookie 也会被设置。有没有人有解决方案?提前谢谢!
【问题讨论】:
标签: cookies razor model-view-controller identityserver4 openid-connect