ASP.NET (OWIN) Identity: How to get UserID from a Web API controller?

You should be able to get user id on both MVC controller and web api controller by same extension method in identity 1.0 RTW package. Here is the extensions from identity package: namespace Microsoft.AspNet.Identity { public static class IdentityExtensions { public static string FindFirstValue(this ClaimsIdentity identity, string claimType); public static string GetUserId(this IIdentity identity); public … Read more

ASP.NET Identity reset password

Or how can I reset without knowing the current one (user forgot password)? If you want to change a password using the UserManager but you do not want to supply the user’s current password, you can generate a password reset token and then use it immediately instead. string resetToken = await UserManager.GeneratePasswordResetTokenAsync(model.Id); IdentityResult passwordChangeResult = … Read more

How to use Windows Active Directory Authentication and Identity Based Claims?

Just hit AD with the username and password instead of authenticating against your DB // POST: /Account/Login [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(model.UserName); if (user != null && AuthenticateAD(model.UserName, model.Password)) { await SignInAsync(user, model.RememberMe); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError(“”, “Invalid username … Read more

ASP.NET Core change EF connection string when user logs in

Create a DbContext factory public static class DbContextFactory { public static Dictionary<string, string> ConnectionStrings { get; set; } public static void SetConnectionString(Dictionary<string, string> connStrs) { ConnectionStrings = connStrs; } public static MyDbContext Create(string connid) { if (!string.IsNullOrEmpty(connid)) { var connStr = ConnectionStrings[connid]; var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>(); optionsBuilder.UseSqlServer(connStr); return new MyDbContext(optionsBuilder.Options); } else { throw … Read more

The entity type ApplicationUser is not part of the model for the current context

I was having this same problem. I’m doing database first development with an EDMX file. If you are using the connection string generated when adding the EDMX file in :base(“EDMXConnString”) you will most likely have this problem. I fixed this by creating a standard connection string that pointed to the database where the ASP.NET Identity … Read more

Why is Asp.Net Identity IdentityDbContext a Black-Box?

The ApplicationDbContext‘s Users and Roles properties are mapped to the AspNetUsers and AspNetRoles tables, and the rest of the entities (Claims, Logins, UserRoles) are mapped automatically via navigation properties. As far as I know, the prefixing of table names with “AspNet” are the only custom mappings in ApplicationDbContext, everything else is just Entity Framework Code … Read more