3

I'm trying to Authenticate to my web application via a chrome extension, at the moment the data is being sent fine but the authentication cookie is not being created / persisting.

Not sure what I'm doing wrong / if I want to gather information pertaining to a logged in user via the chrome extension what would be the best way to persist the login to my web application.

Login Controller

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 extension script

$('#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 Answers0