Configure the authorization server endpoint

EDIT (01/28/2021): AspNet.Security.OpenIdConnect.Server has been merged into OpenIddict as part of the 3.0 update. To get started with OpenIddict, visit documentation.openiddict.com. Okay, let’s recap the different OAuth2 middleware (and their respective IAppBuilder extensions) that were offered by OWIN/Katana 3 and the ones that will be ported to ASP.NET Core: app.UseOAuthBearerAuthentication/OAuthBearerAuthenticationMiddleware: its name was not terribly … Read more

Unauthorised webapi call returning login page rather than 401

Brock Allen has a nice blog post on how to return 401 for ajax calls when using Cookie authentication and OWIN. http://brockallen.com/2013/10/27/using-cookie-authentication-middleware-with-web-api-and-401-response-codes/ Put this in ConfigureAuth method in the Startup.Auth.cs file: app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString(“/Account/Login”), Provider = new CookieAuthenticationProvider { OnApplyRedirect = ctx => { if (!IsAjaxRequest(ctx.Request)) { ctx.Response.Redirect(ctx.RedirectUri); … Read more

How to get the current logged in user ID in ASP.NET Core?

Update in ASP.NET Core Version >= 2.0 In the Controller: public class YourControllerNameController : Controller { private readonly UserManager<ApplicationUser> _userManager; public YourControllerNameController(UserManager<ApplicationUser> userManager) { _userManager = userManager; } public async Task<IActionResult> YourMethodName() { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier) // will give the user’s userId var userName = User.FindFirstValue(ClaimTypes.Name) // will give the user’s userName // For … Read more

How to get current user in asp.net core

User.FindFirst(ClaimTypes.NameIdentifier).Value EDIT for constructor Below code works: public Controller(IHttpContextAccessor httpContextAccessor) { var userId = httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value } Edit for RTM You should register IHttpContextAccessor: public void ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor(); }

How can I change the table names when using ASP.NET Identity?

You can do this easily by modifying the IdentityModel.cs as per the below: Override OnModelCreating in your DbContext then add the following, this will change AspNetUser table to “Users” you can also change the field names the default Id column will become User_Id. modelBuilder.Entity<IdentityUser>() .ToTable(“Users”, “dbo”).Property(p => p.Id).HasColumnName(“User_Id”); or simply the below if you want … Read more

How to extend available properties of User.Identity

Whenever you want to extend the properties of User.Identity with any additional properties like the question above, add these properties to the ApplicationUser class first like so: public class ApplicationUser : IdentityUser { public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this, … Read more

What is ASP.NET Identity’s IUserSecurityStampStore interface?

This is meant to represent the current snapshot of your user’s credentials. So if nothing changes, the stamp will stay the same. But if the user’s password is changed, or a login is removed (unlink your google/fb account), the stamp will change. This is needed for things like automatically signing users/rejecting old cookies when this … Read more