ASP.Net Core 2 authentication












1















I am lost with authentification in ASP.Net Core 2 MVC Applications.
I am working with Core 2 version and it seems there are a lot of changes between version 1 and 2. I have read some tutorials which does not really works.



First of all, here is what i put in Startup.cs in ConfigureServices() method:



services.AddIdentity<MyUserClass, IdentityRole>()
.AddEntityFrameworkStores<MyDatabaseEFContext>()
.AddDefaultTokenProviders();

services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.Cookie.Expiration = TimeSpan.FromDays(150);
options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
options.SlidingExpiration = true;
});


and here is what i put in Configure() method:



app.UseIdentity();


I put this annotation on each action method of each controller:



[Authorize]


And here is what i've done in my post action login method:



[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Index(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}

var claims = new List<Claim> {new Claim(ClaimTypes.Name, model.Login)};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);

await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

return RedirectToAction("Index", "PrivateController");
}


I get this exception when i try to login:




InvalidOperationException: No authentication handler is configured to handle the scheme: Cookies




Any idea of what is wrong ?










share|improve this question

























  • Could be you are missing services.ConfigureApplicationCookie as mentioned in docs docs.microsoft.com/en-us/aspnet/core/security/authentication/…

    – Nkosi
    Nov 29 '17 at 21:04






  • 1





    Are you using ASP.NET Identity or just cookie authentication? Currently, your code is using both.

    – Win
    Nov 29 '17 at 22:22
















1















I am lost with authentification in ASP.Net Core 2 MVC Applications.
I am working with Core 2 version and it seems there are a lot of changes between version 1 and 2. I have read some tutorials which does not really works.



First of all, here is what i put in Startup.cs in ConfigureServices() method:



services.AddIdentity<MyUserClass, IdentityRole>()
.AddEntityFrameworkStores<MyDatabaseEFContext>()
.AddDefaultTokenProviders();

services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.Cookie.Expiration = TimeSpan.FromDays(150);
options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
options.SlidingExpiration = true;
});


and here is what i put in Configure() method:



app.UseIdentity();


I put this annotation on each action method of each controller:



[Authorize]


And here is what i've done in my post action login method:



[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Index(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}

var claims = new List<Claim> {new Claim(ClaimTypes.Name, model.Login)};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);

await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

return RedirectToAction("Index", "PrivateController");
}


I get this exception when i try to login:




InvalidOperationException: No authentication handler is configured to handle the scheme: Cookies




Any idea of what is wrong ?










share|improve this question

























  • Could be you are missing services.ConfigureApplicationCookie as mentioned in docs docs.microsoft.com/en-us/aspnet/core/security/authentication/…

    – Nkosi
    Nov 29 '17 at 21:04






  • 1





    Are you using ASP.NET Identity or just cookie authentication? Currently, your code is using both.

    – Win
    Nov 29 '17 at 22:22














1












1








1


0






I am lost with authentification in ASP.Net Core 2 MVC Applications.
I am working with Core 2 version and it seems there are a lot of changes between version 1 and 2. I have read some tutorials which does not really works.



First of all, here is what i put in Startup.cs in ConfigureServices() method:



services.AddIdentity<MyUserClass, IdentityRole>()
.AddEntityFrameworkStores<MyDatabaseEFContext>()
.AddDefaultTokenProviders();

services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.Cookie.Expiration = TimeSpan.FromDays(150);
options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
options.SlidingExpiration = true;
});


and here is what i put in Configure() method:



app.UseIdentity();


I put this annotation on each action method of each controller:



[Authorize]


And here is what i've done in my post action login method:



[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Index(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}

var claims = new List<Claim> {new Claim(ClaimTypes.Name, model.Login)};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);

await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

return RedirectToAction("Index", "PrivateController");
}


I get this exception when i try to login:




InvalidOperationException: No authentication handler is configured to handle the scheme: Cookies




Any idea of what is wrong ?










share|improve this question
















I am lost with authentification in ASP.Net Core 2 MVC Applications.
I am working with Core 2 version and it seems there are a lot of changes between version 1 and 2. I have read some tutorials which does not really works.



First of all, here is what i put in Startup.cs in ConfigureServices() method:



services.AddIdentity<MyUserClass, IdentityRole>()
.AddEntityFrameworkStores<MyDatabaseEFContext>()
.AddDefaultTokenProviders();

services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.Cookie.Expiration = TimeSpan.FromDays(150);
options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
options.SlidingExpiration = true;
});


and here is what i put in Configure() method:



app.UseIdentity();


I put this annotation on each action method of each controller:



[Authorize]


And here is what i've done in my post action login method:



[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Index(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}

var claims = new List<Claim> {new Claim(ClaimTypes.Name, model.Login)};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);

await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

return RedirectToAction("Index", "PrivateController");
}


I get this exception when i try to login:




InvalidOperationException: No authentication handler is configured to handle the scheme: Cookies




Any idea of what is wrong ?







c# asp.net-core asp.net-core-2.0 asp.net-core-identity






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '17 at 21:19







Bob5421

















asked Nov 29 '17 at 20:39









Bob5421Bob5421

1,22622158




1,22622158













  • Could be you are missing services.ConfigureApplicationCookie as mentioned in docs docs.microsoft.com/en-us/aspnet/core/security/authentication/…

    – Nkosi
    Nov 29 '17 at 21:04






  • 1





    Are you using ASP.NET Identity or just cookie authentication? Currently, your code is using both.

    – Win
    Nov 29 '17 at 22:22



















  • Could be you are missing services.ConfigureApplicationCookie as mentioned in docs docs.microsoft.com/en-us/aspnet/core/security/authentication/…

    – Nkosi
    Nov 29 '17 at 21:04






  • 1





    Are you using ASP.NET Identity or just cookie authentication? Currently, your code is using both.

    – Win
    Nov 29 '17 at 22:22

















Could be you are missing services.ConfigureApplicationCookie as mentioned in docs docs.microsoft.com/en-us/aspnet/core/security/authentication/…

– Nkosi
Nov 29 '17 at 21:04





Could be you are missing services.ConfigureApplicationCookie as mentioned in docs docs.microsoft.com/en-us/aspnet/core/security/authentication/…

– Nkosi
Nov 29 '17 at 21:04




1




1





Are you using ASP.NET Identity or just cookie authentication? Currently, your code is using both.

– Win
Nov 29 '17 at 22:22





Are you using ASP.NET Identity or just cookie authentication? Currently, your code is using both.

– Win
Nov 29 '17 at 22:22












2 Answers
2






active

oldest

votes


















4














In your Configure() method change app.UseIdentity() to:



app.UseAuthentication();


Also, note: If you are using cookies without Identity (as it appears in your Index action):




Invoke the AddAuthentication and AddCookie methods in the
ConfigureServices method:



// If you don't want the cookie to be automatically authenticated and assigned to HttpContext.User, 
// remove the CookieAuthenticationDefaults.AuthenticationScheme parameter passed to AddAuthentication.
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Account/LogIn";
options.LogoutPath = "/Account/LogOff";
});



Additional reading: Migrating Authentication and Identity to ASP.NET Core 2.0






share|improve this answer


























  • a get the same error...

    – Bob5421
    Nov 29 '17 at 21:31






  • 1





    @Bob5421: if you are using cookies without Identity you will need to add services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) in your ConfigureServices. See updated answer and Migrating Authentication and Identity to ASP.NET Core 2.0

    – CalC
    Nov 29 '17 at 21:43













  • In fact i do not understand the difference between cookies and identity. What i want is authentication with a session like in php :)

    – Bob5421
    Nov 29 '17 at 22:29






  • 1





    You should read this Introduction to Identity on ASP.NET Core. It is the line HttpContext.Authentication.SignInAsync that is causing your problem. If you want to use Identity, you should use a SignInManager.SignInAsync() instead.

    – CalC
    Nov 29 '17 at 23:31













  • You are absolutely true. This was the problem. But i do not understand why HttpContext.Authentication.SignInAsync does not work !

    – Bob5421
    Nov 30 '17 at 6:58



















0














I fixed it the way I do my own Logout() action which deletes the authentication cookie and then redirects to the start page. To do it reliably I gave the auth. cookie my own name using the ConfigureServices() method in Startup.cs.



Startup.cs:



    private void ConfigureServices(IServiceCollection services)
{
..

services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);

options.LoginPath = "/Identity/Account/Login";
options.AccessDeniedPath = "/Identity/Account/AccessDenied";
options.SlidingExpiration = true;
options.Cookie.Name = "MyOwnCookieName";
});
...


HomeController.cs:



    [Authorize]
[HttpGet]
public IActionResult Logout()
{
Response.Cookies.Delete("MyOwnCookieName");
return RedirectToAction("Index");
}


Maybe this saves someone some time as I used a lot of time to get there.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47561426%2fasp-net-core-2-authentication%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    In your Configure() method change app.UseIdentity() to:



    app.UseAuthentication();


    Also, note: If you are using cookies without Identity (as it appears in your Index action):




    Invoke the AddAuthentication and AddCookie methods in the
    ConfigureServices method:



    // If you don't want the cookie to be automatically authenticated and assigned to HttpContext.User, 
    // remove the CookieAuthenticationDefaults.AuthenticationScheme parameter passed to AddAuthentication.
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
    options.LoginPath = "/Account/LogIn";
    options.LogoutPath = "/Account/LogOff";
    });



    Additional reading: Migrating Authentication and Identity to ASP.NET Core 2.0






    share|improve this answer


























    • a get the same error...

      – Bob5421
      Nov 29 '17 at 21:31






    • 1





      @Bob5421: if you are using cookies without Identity you will need to add services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) in your ConfigureServices. See updated answer and Migrating Authentication and Identity to ASP.NET Core 2.0

      – CalC
      Nov 29 '17 at 21:43













    • In fact i do not understand the difference between cookies and identity. What i want is authentication with a session like in php :)

      – Bob5421
      Nov 29 '17 at 22:29






    • 1





      You should read this Introduction to Identity on ASP.NET Core. It is the line HttpContext.Authentication.SignInAsync that is causing your problem. If you want to use Identity, you should use a SignInManager.SignInAsync() instead.

      – CalC
      Nov 29 '17 at 23:31













    • You are absolutely true. This was the problem. But i do not understand why HttpContext.Authentication.SignInAsync does not work !

      – Bob5421
      Nov 30 '17 at 6:58
















    4














    In your Configure() method change app.UseIdentity() to:



    app.UseAuthentication();


    Also, note: If you are using cookies without Identity (as it appears in your Index action):




    Invoke the AddAuthentication and AddCookie methods in the
    ConfigureServices method:



    // If you don't want the cookie to be automatically authenticated and assigned to HttpContext.User, 
    // remove the CookieAuthenticationDefaults.AuthenticationScheme parameter passed to AddAuthentication.
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
    options.LoginPath = "/Account/LogIn";
    options.LogoutPath = "/Account/LogOff";
    });



    Additional reading: Migrating Authentication and Identity to ASP.NET Core 2.0






    share|improve this answer


























    • a get the same error...

      – Bob5421
      Nov 29 '17 at 21:31






    • 1





      @Bob5421: if you are using cookies without Identity you will need to add services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) in your ConfigureServices. See updated answer and Migrating Authentication and Identity to ASP.NET Core 2.0

      – CalC
      Nov 29 '17 at 21:43













    • In fact i do not understand the difference between cookies and identity. What i want is authentication with a session like in php :)

      – Bob5421
      Nov 29 '17 at 22:29






    • 1





      You should read this Introduction to Identity on ASP.NET Core. It is the line HttpContext.Authentication.SignInAsync that is causing your problem. If you want to use Identity, you should use a SignInManager.SignInAsync() instead.

      – CalC
      Nov 29 '17 at 23:31













    • You are absolutely true. This was the problem. But i do not understand why HttpContext.Authentication.SignInAsync does not work !

      – Bob5421
      Nov 30 '17 at 6:58














    4












    4








    4







    In your Configure() method change app.UseIdentity() to:



    app.UseAuthentication();


    Also, note: If you are using cookies without Identity (as it appears in your Index action):




    Invoke the AddAuthentication and AddCookie methods in the
    ConfigureServices method:



    // If you don't want the cookie to be automatically authenticated and assigned to HttpContext.User, 
    // remove the CookieAuthenticationDefaults.AuthenticationScheme parameter passed to AddAuthentication.
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
    options.LoginPath = "/Account/LogIn";
    options.LogoutPath = "/Account/LogOff";
    });



    Additional reading: Migrating Authentication and Identity to ASP.NET Core 2.0






    share|improve this answer















    In your Configure() method change app.UseIdentity() to:



    app.UseAuthentication();


    Also, note: If you are using cookies without Identity (as it appears in your Index action):




    Invoke the AddAuthentication and AddCookie methods in the
    ConfigureServices method:



    // If you don't want the cookie to be automatically authenticated and assigned to HttpContext.User, 
    // remove the CookieAuthenticationDefaults.AuthenticationScheme parameter passed to AddAuthentication.
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
    options.LoginPath = "/Account/LogIn";
    options.LogoutPath = "/Account/LogOff";
    });



    Additional reading: Migrating Authentication and Identity to ASP.NET Core 2.0







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 29 '17 at 21:43

























    answered Nov 29 '17 at 21:29









    CalCCalC

    4,92332036




    4,92332036













    • a get the same error...

      – Bob5421
      Nov 29 '17 at 21:31






    • 1





      @Bob5421: if you are using cookies without Identity you will need to add services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) in your ConfigureServices. See updated answer and Migrating Authentication and Identity to ASP.NET Core 2.0

      – CalC
      Nov 29 '17 at 21:43













    • In fact i do not understand the difference between cookies and identity. What i want is authentication with a session like in php :)

      – Bob5421
      Nov 29 '17 at 22:29






    • 1





      You should read this Introduction to Identity on ASP.NET Core. It is the line HttpContext.Authentication.SignInAsync that is causing your problem. If you want to use Identity, you should use a SignInManager.SignInAsync() instead.

      – CalC
      Nov 29 '17 at 23:31













    • You are absolutely true. This was the problem. But i do not understand why HttpContext.Authentication.SignInAsync does not work !

      – Bob5421
      Nov 30 '17 at 6:58



















    • a get the same error...

      – Bob5421
      Nov 29 '17 at 21:31






    • 1





      @Bob5421: if you are using cookies without Identity you will need to add services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) in your ConfigureServices. See updated answer and Migrating Authentication and Identity to ASP.NET Core 2.0

      – CalC
      Nov 29 '17 at 21:43













    • In fact i do not understand the difference between cookies and identity. What i want is authentication with a session like in php :)

      – Bob5421
      Nov 29 '17 at 22:29






    • 1





      You should read this Introduction to Identity on ASP.NET Core. It is the line HttpContext.Authentication.SignInAsync that is causing your problem. If you want to use Identity, you should use a SignInManager.SignInAsync() instead.

      – CalC
      Nov 29 '17 at 23:31













    • You are absolutely true. This was the problem. But i do not understand why HttpContext.Authentication.SignInAsync does not work !

      – Bob5421
      Nov 30 '17 at 6:58

















    a get the same error...

    – Bob5421
    Nov 29 '17 at 21:31





    a get the same error...

    – Bob5421
    Nov 29 '17 at 21:31




    1




    1





    @Bob5421: if you are using cookies without Identity you will need to add services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) in your ConfigureServices. See updated answer and Migrating Authentication and Identity to ASP.NET Core 2.0

    – CalC
    Nov 29 '17 at 21:43







    @Bob5421: if you are using cookies without Identity you will need to add services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) in your ConfigureServices. See updated answer and Migrating Authentication and Identity to ASP.NET Core 2.0

    – CalC
    Nov 29 '17 at 21:43















    In fact i do not understand the difference between cookies and identity. What i want is authentication with a session like in php :)

    – Bob5421
    Nov 29 '17 at 22:29





    In fact i do not understand the difference between cookies and identity. What i want is authentication with a session like in php :)

    – Bob5421
    Nov 29 '17 at 22:29




    1




    1





    You should read this Introduction to Identity on ASP.NET Core. It is the line HttpContext.Authentication.SignInAsync that is causing your problem. If you want to use Identity, you should use a SignInManager.SignInAsync() instead.

    – CalC
    Nov 29 '17 at 23:31







    You should read this Introduction to Identity on ASP.NET Core. It is the line HttpContext.Authentication.SignInAsync that is causing your problem. If you want to use Identity, you should use a SignInManager.SignInAsync() instead.

    – CalC
    Nov 29 '17 at 23:31















    You are absolutely true. This was the problem. But i do not understand why HttpContext.Authentication.SignInAsync does not work !

    – Bob5421
    Nov 30 '17 at 6:58





    You are absolutely true. This was the problem. But i do not understand why HttpContext.Authentication.SignInAsync does not work !

    – Bob5421
    Nov 30 '17 at 6:58













    0














    I fixed it the way I do my own Logout() action which deletes the authentication cookie and then redirects to the start page. To do it reliably I gave the auth. cookie my own name using the ConfigureServices() method in Startup.cs.



    Startup.cs:



        private void ConfigureServices(IServiceCollection services)
    {
    ..

    services.ConfigureApplicationCookie(options =>
    {
    // Cookie settings
    options.Cookie.HttpOnly = true;
    options.ExpireTimeSpan = TimeSpan.FromMinutes(30);

    options.LoginPath = "/Identity/Account/Login";
    options.AccessDeniedPath = "/Identity/Account/AccessDenied";
    options.SlidingExpiration = true;
    options.Cookie.Name = "MyOwnCookieName";
    });
    ...


    HomeController.cs:



        [Authorize]
    [HttpGet]
    public IActionResult Logout()
    {
    Response.Cookies.Delete("MyOwnCookieName");
    return RedirectToAction("Index");
    }


    Maybe this saves someone some time as I used a lot of time to get there.






    share|improve this answer




























      0














      I fixed it the way I do my own Logout() action which deletes the authentication cookie and then redirects to the start page. To do it reliably I gave the auth. cookie my own name using the ConfigureServices() method in Startup.cs.



      Startup.cs:



          private void ConfigureServices(IServiceCollection services)
      {
      ..

      services.ConfigureApplicationCookie(options =>
      {
      // Cookie settings
      options.Cookie.HttpOnly = true;
      options.ExpireTimeSpan = TimeSpan.FromMinutes(30);

      options.LoginPath = "/Identity/Account/Login";
      options.AccessDeniedPath = "/Identity/Account/AccessDenied";
      options.SlidingExpiration = true;
      options.Cookie.Name = "MyOwnCookieName";
      });
      ...


      HomeController.cs:



          [Authorize]
      [HttpGet]
      public IActionResult Logout()
      {
      Response.Cookies.Delete("MyOwnCookieName");
      return RedirectToAction("Index");
      }


      Maybe this saves someone some time as I used a lot of time to get there.






      share|improve this answer


























        0












        0








        0







        I fixed it the way I do my own Logout() action which deletes the authentication cookie and then redirects to the start page. To do it reliably I gave the auth. cookie my own name using the ConfigureServices() method in Startup.cs.



        Startup.cs:



            private void ConfigureServices(IServiceCollection services)
        {
        ..

        services.ConfigureApplicationCookie(options =>
        {
        // Cookie settings
        options.Cookie.HttpOnly = true;
        options.ExpireTimeSpan = TimeSpan.FromMinutes(30);

        options.LoginPath = "/Identity/Account/Login";
        options.AccessDeniedPath = "/Identity/Account/AccessDenied";
        options.SlidingExpiration = true;
        options.Cookie.Name = "MyOwnCookieName";
        });
        ...


        HomeController.cs:



            [Authorize]
        [HttpGet]
        public IActionResult Logout()
        {
        Response.Cookies.Delete("MyOwnCookieName");
        return RedirectToAction("Index");
        }


        Maybe this saves someone some time as I used a lot of time to get there.






        share|improve this answer













        I fixed it the way I do my own Logout() action which deletes the authentication cookie and then redirects to the start page. To do it reliably I gave the auth. cookie my own name using the ConfigureServices() method in Startup.cs.



        Startup.cs:



            private void ConfigureServices(IServiceCollection services)
        {
        ..

        services.ConfigureApplicationCookie(options =>
        {
        // Cookie settings
        options.Cookie.HttpOnly = true;
        options.ExpireTimeSpan = TimeSpan.FromMinutes(30);

        options.LoginPath = "/Identity/Account/Login";
        options.AccessDeniedPath = "/Identity/Account/AccessDenied";
        options.SlidingExpiration = true;
        options.Cookie.Name = "MyOwnCookieName";
        });
        ...


        HomeController.cs:



            [Authorize]
        [HttpGet]
        public IActionResult Logout()
        {
        Response.Cookies.Delete("MyOwnCookieName");
        return RedirectToAction("Index");
        }


        Maybe this saves someone some time as I used a lot of time to get there.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 6:13









        MarcMarc

        1,80111528




        1,80111528






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47561426%2fasp-net-core-2-authentication%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python