通过Chrome扩展程序进行Cookie身份验证

时间:2019-04-09 10:37:39

标签: c# .net asp.net-core-2.0 cookie-authentication

我正在尝试通过chrome扩展名对我的Web应用程序进行身份验证,此刻数据发送正常,但身份验证cookie并未创建/持久化。

不确定我做错了什么/如果我想通过chrome扩展程序收集与已登录用户有关的信息,那是将登录持续保存到我的Web应用程序的最佳方法。

登录控制器

public bool Login()
        {
            try
            {
  Models.DatabaseModels.UserModel user = new Models.DatabaseModels.UserModel
                {
                    Username = Request.Headers["username"],
                    PasswordHash = PassHash(Request.Headers["password"])
                };

               if(_userRepo.Validate(user){
                var claims = new[] { new Claim(ClaimTypes.Name, user.Username),
                new Claim(ClaimTypes.Role, "User") };

                var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

                var authProperties = new AuthenticationProperties
                {
                    AllowRefresh = true,
                    ExpiresUtc = DateTimeOffset.Now.AddDays(1),
                    IsPersistent = true,
                };

                HttpContext.SignInAsync(
                    CookieAuthenticationDefaults.AuthenticationScheme,
                    new ClaimsPrincipal(identity), authProperties);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
}
        }

startup.cs

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(o => o.LoginPath = new PathString("/"));

chrome扩展脚本

$('#registerButton').on('click', function(e){
$.ajax({
    url:  'https://localhost:44327/Test/Login',
    headers: {
        username: $('#username').val(),
        password: $('#password').val()
    },
    type: "POST",
}).done(function (data){

});

});

0 个答案:

没有答案