81993

Asp.net vNext Cookie Authentication

Question:

Our website needs to authenticate against a third party webservice and create a cookie. We don't need to store the membership information. I have the following code in Startup.cs

app.UseCookieAuthentication(options => { options.AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.LoginPath = new PathString("/User/Login"); options.CookieName = "GEMSNCID"; options.ExpireTimeSpan = new System.TimeSpan(1, 0, 0); });

and the login method is

var claims = new[] { new Claim(ClaimTypes.Name, model.UserName), new Claim(ClaimTypes.Country, "USA") }; var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); ClaimsPrincipal principal = new ClaimsPrincipal(identity); Context.Response.SignIn(CookieAuthenticationDefaults.AuthenticationScheme, principal); return RedirectToAction("Index", "Home");

This is not working. Can someone please help.

Answer1:

I think something change on the new beta 4 version. I will try this code is working on my my sample application

var claims = new[] { new Claim(ClaimTypes.Name, model.UserName), new Claim(ClaimTypes.Country, "USA") }; var user = this.User as ClaimsPrincipal; var identity = user.Identities.Where(x => x.AuthenticationType == CookieAuthenticationDefaults.AuthenticationScheme).FirstOrDefault(); if (identity == null) { identity = new ClaimsIdentity(claims.ToArray(), CookieAuthenticationDefaults.AuthenticationScheme); user.AddIdentity(identity); } else identity.AddClaims(claims); if (this.Context.Response.HttpContext.User.Claims.Count() > 1) this.Context.Response.SignIn(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

Recommend

  • Issue in adding Xsrf-Token in an Angular 6
  • Parse a soap XML to a C# class
  • IdentityServer4 cookie expiration
  • Cookie adding another entry instead of replacing existing value
  • Angular 6 does not add X-XSRF-TOKEN header to http request
  • Forms Authentication for a Silverlight application in Windows Azure
  • Cookie Authentication Early Expiration
  • When I used CookieLocaleResolver, I can set invalid cookie to crash spring web application
  • Permission screen appears every time
  • Mod_rewrite: Force SSL on specific pages. Add www on none-secure pages
  • Eclipse : Unknown Source Java SQL Exception
  • Unable to generate call to cell phone using asterisk
  • Angular2 How to display localStorage value inside HTML5 template?
  • firebase unauth with google doesn't allow change of user
  • .htaccess rewrite: subdomain as GET parameter and filepath afterdomain intact
  • Bullet-Proof ACL using AngularJS
  • Wordpress and Drupal SSO/Single Sign On
  • Is there a way to use Firebase Legacy console from new account
  • IIS Equivalent of “proxy_set_header X-Forwarded-Proto https;”
  • autotest and rspec giving different answers
  • How to use HTTP Authentication with PHP and then run the entered data against a database?
  • Authentication - JavaScript - Logout issue
  • Laravel 5.1 + PHPunit - API test returns always invalid argument error foreach
  • Selenium and Google - How do you use cookies?
  • jQuery and Uploadify session in the php file
  • Error on Wordpress Admin URL after moving from localhost to remote
  • When use gae remote error occured : com.google.appengine.repackaged.com.google.api.client.http.HttpR
  • Sails.js API passport.js authentication
  • Android application not restoring state when installed from .apk, works fine from eclipse
  • Express JS Display Data By ID
  • Kivy, TypeError: 'NoneType' object has no attribute '__getitem__'
  • Meteor.. accounts- password— Create account on client without login
  • ASP.NET windows authentication should always ask for credentials
  • Is it better to have roles as a column on my users table, or do it through join tables (Roles &
  • read values from form post in jquery or javascript
  • (Tcl/Expect) clear screen after exit
  • Django rest serializer Breaks when data exists
  • Recording logins for password protected directories
  • Is there any way to access browser form field suggestions from JavaScript?
  • Getting error when using KSoap library to consume .NET web services