Get the UserManager instance outside of ASP.NET web application? - c#

I have an ASP.NET MVC 5 web app with ASP.NET Identity (Individual Accounts). But I need to be able to register new users from a console app.
So I'm moving some of the ASP.NET Identity classes from the web app into a class library to be shared between the web app and the CLI.
I have successfully moved the following:
public class PortalDbContext : IdentityDbContext<PortalUser>
{
public PortalDbContext(string connectionString)
: base(connectionString, throwIfV1Schema: false)
{
}
public static PortalDbContext Create(string connectionString)
{
return new PortalDbContext(connectionString);
}
}
public class PortalUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<PortalUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class PortalUserManager : UserManager<PortalUser>
{
public PortalUserManager(IUserStore<PortalUser> store) : base(store)
{
}
public async Task<IdentityResult> RegisterUser(string email, string password)
{
PortalUser user = new PortalUser { UserName = email, Email = email };
return await this.CreateAsync(user, password);
}
}
But I have no idea where to get the IUserStore<PortalUser> the PortalUserManager needs from.
In the web app, this manager is retrieved from HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>() which I clearly can't use in a class library.

Take a look at the OwinRequestScopeContext nuget package. It allows you to make use of a context without a dependency on System.Web. I'll add the example from the current readme for the sake of not having a link-only answer:
# Usage
// using Owin; you can use UseRequestScopeContext extension method.
// enabled timing is according to Pipeline.
// so I recommend enable as far in advance as possible.
app.UseRequestScopeContext();
app.UseErrorPage();
app.Run(async _ =>
{
// get global context like HttpContext.Current.
var context = OwinRequestScopeContext.Current;
// Environment is raw Owin Environment as IDictionary<string, object>.
var __ = context.Environment;
// optional:If you want to change Microsoft.Owin.OwinContext, you can wrap.
new Microsoft.Owin.OwinContext(context.Environment);
// Timestamp is request started(correctly called RequestScopeContextMiddleware timing).
var ___ = context.Timestamp;
// Items is IDictionary<string, object> like HttpContext.Items.
// Items is threadsafe(as ConcurrentDictionary) by default.
var ____ = context.Items;
// DisposeOnPipelineCompleted can register dispose when request completed(correctly RequestScopeContextMiddleware underling Middlewares finished)
// return value is cancelToken. If call token.Dispose() then canceled register.
var cancelToken = context.DisposeOnPipelineCompleted(new TraceDisposable());
// OwinRequestScopeContext over async/await also ConfigureAwait(false)
context.Items["test"] = "foo";
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
var _____ = OwinRequestScopeContext.Current.Items["test"]; // foo
await Task.Run(() =>
{
// OwinRequestScopeContext over new thread/threadpool.
var ______ = OwinRequestScopeContext.Current.Items["test"]; // foo
});
_.Response.ContentType = "text/plain";
await _.Response.WriteAsync("Hello OwinRequestScopeContext! => ");
await _.Response.WriteAsync(OwinRequestScopeContext.Current.Items["test"] as string); // render foo
});

I've ended up adding a static method Create(string connectionString) to PortalUserManager that will create the UserStore and DbContext and return a new instance of the manager.
public class PortalDbContext : IdentityDbContext<PortalUser>
{
public PortalDbContext(string connectionString)
: base(connectionString, throwIfV1Schema: false)
{
}
public static PortalDbContext Create(string connectionString)
{
return new PortalDbContext(connectionString);
}
}
public class PortalUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<PortalUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class PortalUserManager : UserManager<PortalUser>
{
public PortalUserManager(IUserStore<PortalUser> store) : base(store)
{
}
public static PortalUserManager Create(string connectionString)
{
UserStore<PortalUser> userStore = new UserStore<PortalUser>(PortalDbContext.Create(connectionString));
PortalUserManager manager = new PortalUserManager(userStore);
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<PortalUser>(manager)
{
AllowOnlyAlphanumericUserNames = true,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
return manager;
}
public async Task<IdentityResult> RegisterUser(string email, string password)
{
PortalUser user = new PortalUser { UserName = email, Email = email };
return await this.CreateAsync(user, password);
}
}

Related

Customizing .net Identity 2

I have an MVC5 application with .net 4.7.2 and I'm trying to have my customized version of .net Identity classes. I've followed the steps in this tutorial, with some changes here and there, because my project rely on a set of WCF services to communicate with the database, so I don't have direct connection to the database:
overview-of-custom-storage-providers-for-aspnet-identity
So mainly I've created my own MyUserStore which implements IUserStore and IUserRoleStore, and I've added this to Startup.cs:
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext<MyUserManager>(MyUserManager.Create);
}
Now the problem that the IsInRole methods are not getting invoked, and also I had break point for methods like FindByIdAsync, FindByNameAsync and IsInRoleAsync and nothing got invoked, even though I've added
[Authorize(Roles ="TestRole")] attribute to couple actions and has tried to invoke this explicitly:
this.User.IsInRole("TestRole");
What I'm missing here to have it work in the proper way?
Edit #1 - adding the whole code:
Here under is the User Store related classes:
public class MyUserStore :
IUserStore<MyIdentityUser, int>,
IUserRoleStore<MyIdentityUser, int>,
IQueryableUserStore<MyIdentityUser, int>,
IDisposable
{
public Task CreateAsync(MyIdentityUser account)
{
// Add account to DB...
return Task.FromResult<object>(null);
}
public Task<MyIdentityUser> FindByIdAsync(int accountId)
{
// Get account from DB and return it
return Task.FromResult<MyIdentityUser>(account);
}
public Task<MyIdentityUser> FindByNameAsync(string userName)
{
// Get account from DB and return it
return Task.FromResult<MyIdentityUser>(account);
}
public Task UpdateAsync(MyIdentityUser account)
{
// Update account in DB
return Task.FromResult<object>(null);
}
public Task AddToRoleAsync(MyIdentityUser account, string roleName)
{
throw new NotImplementedException("UserStore.AddToRoleAsync");
return Task.FromResult<object>(null);
}
public Task<IList<string>> GetRolesAsync(MyIdentityUser account)
{
// TODO: Check if important to implement
throw new NotImplementedException("UserStore.GetRolesAsync");
}
public Task<bool> IsInRoleAsync(MyIdentityUser account, string task)
{
// Return true if has permission (not getting invoked)
return Task.FromResult<bool>(hasPermission);
}
public Task RemoveFromRoleAsync(MyIdentityUser account, string task)
{
throw new NotImplementedException("UserStore.RemoveFromRoleAsync");
}
public Task DeleteAsync(MyIdentityUser account)
{
// Delete user from DB
return Task.FromResult<Object>(null);
}
}
public class MyUserRole : IdentityUserRole<int> { }
public class MyUserClaim : IdentityUserClaim<int> { }
public class MyUserLogin : IdentityUserLogin<int> { }
public class MyIdentityUser : IdentityUser<int, MyUserLogin, MyUserRole, MyUserClaim>
{
public MyIdentityUser(int id)
{
Id = id;
}
// My extra account's properties
}
}
And here is the UserManager class:
public class MyUserManager : UserManager<MyIdentityUser, int>
{
public MyUserManager(IUserStore<MyIdentityUser, int> store)
: base(store) { }
public static MyUserManager Create(IdentityFactoryOptions<MyUserManager> options, IOwinContext context)
{
///Calling the non-default constructor of the UserStore class
var manager = new MyUserManager(new MyUserStore());
manager.UserValidator = new UserValidator<MyIdentityUser, int>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = false;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 3;
// You can write your own provider and plug it in here.
return manager;
}
}
And maybe this is the most important part, the login controller:
var manager = HttpContext.GetOwinContext().GetUserManager<MyUserManager>();
//check for credentials before sign in ..
var result = manager.CheckPasswordAsync(vm.userName, vm.password, ref state);
if(result.Result)
{
FormsAuthentication.SetAuthCookie(vm.userName, vm.rememberMe);
return Redirect("~/");
}
Regards,

Add method to ApplicationUserManager

First time I use ASP.NET Identity and I probably miss something.
I know how to use ApplicationUserManager (my class extending UserManager) but I want to create a method inside of it that use UserManager methods because I don't want to repeat code.
Calling "base" doesn't work.
EDIT: the "base" didn't work because I had the method as static (I don't know why I wrote that).
Now it doesn't give me errors but if I try to call it from my Web API Controller I get the "Does not contain a definition of ..." error.
ApplicationUserManager:
namespace BLL
{
// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false,
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
// You can write your own provider and plug it in here.
manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
{
MessageFormat = "Your security code is {0}"
});
manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
{
Subject = "Security Code",
BodyFormat = "Your security code is {0}"
});
manager.EmailService = new EmailService();
manager.SmsService = new SmsService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
public async Task<int> RegistraPuntoScan(string userId, string sitoVisitato)
{
var user = await base.FindByIdAsync(userId);
if(user != null)
{
var s = new Stringa(sitoVisitato);
if (!user.URLVisitati.Contains(s))
{
user.Punti++;
user.URLVisitati.Add(s);
await base.UpdateAsync(user);
return 1;
}
else
{
return 2;
}
}
else
{
return 3;
}
}
}
}
Web API Controller:
namespace MyProject.Controllers.API
{
[CustomAuthorization]
public class PuntiController : ApiController
{
private ApplicationUserManager _userManager;
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
[HttpPost]
public IHttpActionResult RegistraPuntoScan(RegisterPointScanVm vm)
{
ClaimsPrincipal principal = Request.GetRequestContext().Principal as ClaimsPrincipal;
var idUtente = ClaimsPrincipal.Current.Identity.GetUserId();
var user = UserManager.FindById(idUtente);
switch(UserManager.RegistraPuntoScan(idUtente, vm.ScannedURL))
{
case 1:
return Ok();
case 2:
return Conflict();
case 3:
return BadRequest();
}
return BadRequest();
}
}
}
I solved the problem.
I was unable to call the new method because I have the ApplicationUserManager in another project and I probably forgot to delete the default ApplicationUserManager n IdentityConfig.cs, or VisualStudio created it, I don't know.
I deleted IdentityConfig (I have all the classes in other projects) and referenced the right one, now everything works.

The entity type ApplicationUser is not part of the model : Using Webforms

I have reviewed several answers to this on StackOverflow, and I have applied and compared with no answer to my particular case. Here is what I have up to this point:
*****In Web.Config*****
<add name="DefaultConnection" connectionString="Data Source=LAPTOP-2UA8GL6L\SQLEXPRESS;Initial Catalog=FHSF_DEV;Integrated Security=True" providerName="System.Data.SqlClient" />
in IdentityModels.cs
public class ApplicationUser : IdentityUser
{
public DateTime BirthDate { get; set; }
public string City { get; set; }
public string State { get; set; }
public int UserDetailID { get; set; }
public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
{
return Task.FromResult(GenerateUserIdentity(manager));
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
In IdentityConfig.cs
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
return Task.FromResult(0);
}
}
public class SmsService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your SMS service here to send a text message.
return Task.FromResult(0);
}
}
// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
}
//////// Commented out the rest of the code within this class //////
public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore(context.Get()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
// You can write your own provider and plug it in here.
//manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
//{
// MessageFormat = "Your security code is {0}"
//});
//manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
//{
// Subject = "Security Code",
// BodyFormat = "Your security code is {0}"
//});
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = false;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
manager.EmailService = new EmailService();
manager.SmsService = new SmsService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
And, in the calling program where the error takes place
if (IsValid)
{
// Validate the user's email address
//var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
//ApplicationUser user = manager.FindByName(Email.Text);
/////////////////////////////////////////////////////////////////
//var userStore = new UserStore<IdentityUser>();
//var userManager = new UserManager<IdentityUser>(userStore);
//IdentityUser user = userManager.FindByEmail(Email.Text);
var provider = new DpapiDataProtectionProvider("FHSFReset");
ApplicationDbContext context = ApplicationDbContext.Create();
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
///////////////////////////////////////////////////////////
// The error occurs below /////////////////////////////////
///////////////////////////////////////////////////////////
ApplicationUser user = userManager.FindByEmail(Email.Text);
///////////////////////////////////////////////////////////
// Error msg: System.InvalidOperationException: 'The entity
// type ApplicationUser is not part of the model for the
// current context.'
///////////////////////////////////////////////////////////
if (user == null)
{
FailureText.Text = "The user either does not exist or is not confirmed.";
ErrorMessage.Visible = true;
return;
}
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
// Send email with the code and the redirect to reset password page
userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(
provider.Create("EmailRestPW"));
NOTE: I am utilizing Identity, Entity Framework (I think) and OWIN. I am attempting to keep the Authorization/Authentication tables within my database and the tables are there. I (as is shown above) have added customized fields in AspNetUsers and should be reflected in the connectivity.
Is there anything apparent that indicates I have set this up incorrectly such that the error is occurring?

How can I delete an asp.net identity 2.2.1 user in MVC5?

I am using asp.net identity 2.2.1 and I want to delete a user when he/she tries to hit a specific action method in one of none account controllers. Being into many SO question each of them points to a version dependent solution and frankly I couldn't find a to the point answer.
Why there is no plain and simple documentation on deleting an identity user and most importantly why this feature is not part of the identity it self?
Please note that I am using individual user accounts for external logins no local login is allowed.
My identity.cofig files looks like below:
namespace SocialManager
{
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
return Task.FromResult(0);
}
}
public class SmsService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your SMS service here to send a text message.
return Task.FromResult(0);
}
}
// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = false
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
// You can write your own provider and plug it in here.
manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
{
MessageFormat = "Your security code is {0}"
});
manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
{
Subject = "Security Code",
BodyFormat = "Your security code is {0}"
});
manager.EmailService = new EmailService();
manager.SmsService = new SmsService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
// Configure the application sign-in manager which is used in this application.
public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
: base(userManager, authenticationManager)
{
}
public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
{
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
}
public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
{
return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
}
}
}
UserManager has method that looks like this:
public virtual async Task<IdentityResult> DeleteAsync(TUser user)
Use it to delete user record.
Update:
here is how to delete a user:
// id is id of the user to be deleted.
var user = await userManager.FindByIdAsync(id); //use async find
var result = await userManager.DeleteAsync(user);
if (result.Succeeded)
{
// user is deleted
}

Auto Generated id lost asp.net identity

I have been digging into asp.net identity for few days now , and I'm trying to extend and customize the Identity
I created a custom class out of everything and it's working so far
which means I Extended all identity classes:
public class User : IdentityUser<string, UserLogin, UserRole, UserClaim>, IUser, IUser<string>
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
you can see that all classes in the User class are my classes,
I did that for UserStore, RoleStore, UserManager , RoleManager:
public class HEUserManager : UserManager<User>
{
public HEUserManager(IUserStore<User> store)
: base(store)
{
}
public static HEUserManager Create(IdentityFactoryOptions<HEUserManager> options, IOwinContext context)
{
var manager = new HEUserManager(new HEUserStore(context.Get<HEDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<User>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
// You can write your own provider and plug it in here.
manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<User>
{
MessageFormat = "Your security code is {0}"
});
manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<User>
{
Subject = "Security Code",
BodyFormat = "Your security code is {0}"
});
manager.EmailService = new EmailService();
manager.SmsService = new SmsService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
// Configure the application sign-in manager which is used in this application.
public class HESignInManager : SignInManager<User, string>
{
public HESignInManager(HEUserManager userManager, IAuthenticationManager authenticationManager)
: base(userManager, authenticationManager)
{
}
public override Task<ClaimsIdentity> CreateUserIdentityAsync(User user)
{
return user.GenerateUserIdentityAsync((HEUserManager)UserManager);
}
public static HESignInManager Create(IdentityFactoryOptions<HESignInManager> options, IOwinContext context)
{
return new HESignInManager(context.GetUserManager<HEUserManager>(), context.Authentication);
}
}
public class HEUserStore : UserStore<User, Role, string, UserLogin, UserRole, UserClaim>, IUserStore<User>, IUserStore<User,string>, IDisposable
{
public HEUserStore(DbContext context) : base(context)
{
}
}
public class HERoleManager : RoleManager<Role, string>
{
public HERoleManager(IRoleStore<Role, string> store) : base(store)
{
}
}
public class HERoleStore : RoleStore<Role, string, UserRole>
{
public HERoleStore(DbContext context) : base(context)
{
}
}
it's working so far, but before customizing this , crating a user using the user manager is very straight forward , and created a user for me in the database
but now I have to put the user id manually every time , otherwise I'm getting this error
An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in mscorlib.dll but was not handled in user code
Additional information: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
is this normal or did i do something wrong? if it's normal , how can I generate the IDs automatically again?

Categories

Resources