Scaffolding ApplicationUser - asp.net-mvc-5

I'm using the default template for ASP.NET MVC 5 with EF 6. It generates the Identity model as
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, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
I would like to be able to manage and edit the list of users, so I scaffolded the identity model and it added the following line in the ApplicationDbContext class.
public System.Data.Entity.DbSet<QProj.Models.ApplicationUser> ApplicationUsers { get; set; }
I know this is wrong, but I'm not sure how to resolve this.

Here are my steps if you want to manage AspNetUsers table that was generated for you when you enable migrations with all the default settings in place.
First create your controller using MVC 5 Controller with views, using
EFL
Add your controller with the following settings. Use async controller actions should not be selected/checked here since Users DbSet doesn't support async unless we will be using UserManager. That would be a different topic.
Go to the generated controller and rename all instance of ApplicationUsers to Users
Lastly you can now safely delete ApplicationUsers on IdentityModel (on my case, it was line 37)
We need to remove the line 37 as I shown on the last picture since it will fail to build when you run enable-migrations. Since IdentityDbContext<T> itself already contains Users property. So no need to add additional DbSet under ApplicationDbContext class or any class that you declare for that matter. This property by the way was automatically added when you Scaffold ApplicationUser.

These are steps made me successfully scaffolding ApplicationUser:
Refactor ApplicationUser name to something else, like MyAppUser,
Add Controller with EntityFramework, using model MyappUser, this will successfully scaffolding MyAppUser,
The Scaffolding will create property DbSet<MyAppUser> MyAppUsers for ApplicationDbContext, delete it
In newly created controller, change all occurence of db.MyAppUsersto db.Users
Then you're good to go, hope this helps

Related

Customization Default library

Could anybody help me with acumatica? I have an application, witch connect to acumatica with acumatica libraries: Auth, Default_18.200.001, RESTClient from here: https://github.com/Acumatica/AcumaticaRESTAPIClientForCSharp I am using it like submodules - if somesings will change on git - I always can update it
I needed to add custom fields to the Project form. I did it - added fields, posted changes. After that, I created a new endpoint inheriting it from default endpoint - and the new fields became available to me when working with the Project entity. I achieved this by creating classes that inherit from api and the base model with overriding the GetEntityName() method.
If I need to change the fields, I will have to create new inheritance classes.
Perhaps there is some standard way to support customization fields? And I just didn't find it.
My Api extension:
public class ProjectExtApi : EntityAPI<ProjectExt>
{
public ProjectExtApi(Configuration configuration) : base(configuration)
{ }
protected override string GetEntityName()
{
return "Project"; //base code: return typeof(EntityType).Name;
}
}
and model:
[DataContract]
public class ProjectExt : Project
{
[DataMember(Name = "TestDate", EmitDefaultValue = false)]
public DateTimeValue TestDate { get; set; }
}
My answer is related to custom fields inside Acumatica product. Custom fields should be added with DAC extensions (PXCacheExtension class), example:
public class MyContractExt : PXCacheExtension<Contract>
{
#region UsrDateTime
[PXDBDateAndTime]
public virtual DateTime? UsrDateTime { get; set; }
public abstract class usrDateTime : BqlDateTime.Field<usrDateTime> { }
#endregion
}
Note that PMProject DAC in Acumatica is inheriting from Contract DAC and the field will be persisted in the Contract table.
If the field requires to be persisted in database (with PXDBxyz.. attribute instead of PXxyz..) it requires a database script.
It is recommended to add custom field with a customization. When creating a custom field in the Data Access section of a customization the wizard will automatically create the database script to add the column to the table.
External link, tutorial:
https://www.acu-connect.com/2019/02/27/customizing-acumatica-erp-adding-a-new-field-to-an-existing-screen/

How to use multiple model in Controller Type "MVC5 Controller with View using Entity Framework"

I've created Entire my project with Model from ORACLE and Then Controller with Type "MVC5 Controller with View using Entity Framework".
Now the requirement is to consolidate new model in my existing View, How do i don that ?
I need to know how to add multiple Model in Single Controller/View.
Steps/Screen Shots would be appreciated.
Here are some useful examples and along the lines of what I was going to suggest:
https://www.c-sharpcorner.com/UploadFile/ff2f08/multiple-models-in-single-view-in-mvc/
MVC 5 Multiple Models in a Single View
In short, use a ViewModel class to bundle multiple model classes to send to the view.
A snippet from the accepted answer of the referenced stackoverflow Link:
public class ToPage
{
public WebsiteTheme WebsiteTheme{ get; set; }
public User User { get; set; }
public ToPage() {
websiteTheme = new WebsiteTheme();
user = new User();
}
}
Just use this template to get started.
https://youtu.be/912q3TEF25U

Regular Expression to validate ApplicationUser email address in ASP.net MVC5

I am creating a webapp in asp.net MVC5 which will only allow people with a certain domain to sign up.
From what I understand the easiest way is to use a regular expression. Is there any way to use Data Annotations to change the model, or do I have to play around with the view to achieve this?
Probably a bit too late now but I would suggest as follows:
I've have an app in which I need to modify some of ApplicationUser properties, I am trying something which haven't tested on real life yet, but my Unit Tests are passing so far, it could be useful for you.
Override Email property in ApplicationUser, and add a custom validation annotation:
[ValidateEmailDomain(ErrorMessage = "Not a valid email domain.")]
public override string Email { get; set; }
ValidateEmailDomain code:
using System.ComponentModel.DataAnnotations;
using System.Web;
namespace WATHEVER
{
public class ValidateEmailDomain: RequiredAttribute
{
public override bool IsValid(object value)
{
//code
}
}
}
Note that as ValidateEmailDomain inherits from RequiredAttribute it will become obviously obligatory, if you don't want it that way, you could validate it also when it's null.
Sorry for my English :/
with MVC 5 you're using ASPNET Identiy, and the basic properties are encapsulate inside IdentityUser, so for your case, you cannot have access direct to the field email, but yuo can use EF Fluent API to add some rules, the but part is that regularexpression attribute is not part of the EF Fluent API.
If you need to see how can you use EF Fluent API with ASPNET Identity:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{ }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//Define rules
modelBuilder.Entity<IdentityUser>()
.Property(u => u.Email).IsRequired();
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Regards,

Understanding Asp.Net Identity key points

I am an Asp.net developer but very much new to the Asp.net Identity framework. I have been studying the sample application and followed some tutorials too on Identity but still I am not able to grasp the concept completely. I have very firm grip over Asp.net membership but Identity seems nothing like membership. I will explain what I have done so far.
I am creating a simple application in which I am following code first approach. I have created entity model for User which inherits from IdentityUser and has some extra fields. Below is entity model for User.
public class User : IdentityUser
{
public int? CompanyID { get; set; }
public bool? CanWork { get; set; }
public bool? CanSearch { get; set; }
public Company Company { get; set; }
}
Now in the examples people use the name ApplicationUser but for my own purpose I have used name User. Also there is a method in User or ApplicationUser model which is,
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager)
{
CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
I am unable to understand the purpose of this method. Also from an example I have used the following model for Role,
public class Role : IdentityRole
{
public Role()
{
}
public Role(string roleName, string description)
: base(roleName)
{
this.Description = description;
}
public string Description { get; set; }
}
I understand that an extra field is added but I am unable to understand the purpose of overloaded constructor.
The above mentioned confusions are secondary. My primary confusion is that I am familiar that when I create entity models I use DbSet and DbContext and when I call any entity framework method to access the database, the database is created/drop created whichever scheme I am following.
In Identity which method is responsible for creating the Identity tables in the database? I have a IdentityConfig file in which I declare ApplicationUserManager and ApplicationSignInManager. I have also a Startup file. Previously I had only one Startup file in the App_Start folder and when I run the application and tried to accessed any Identity methods it gave me error and was not creating database. I then made the class as partial and created another partial class with same name at the root and then the exception was gone and tables were created. So Startup class is responsible for creating Identity tables? There are extra columns created automatically in the AspNetUsers like PhoneNumber, PhoneNumberConfirmed, TwoFactorEnabled. I don't need these extra columns. Can I remove these? Can I change the names of the Identity tables that are created?
I know these are very basic questions and not one question at all but if I was unable to find some basic tutorial or example for beginners then it would be very beneficial. What I have found are describing those things which I don't need or making me confuse. I want to understand and have control how Identity should work in my application but till now it seems to me that neither I am grasping it completely and nor being able to make is adjustable to my needs. Its like tutorials and example are teaching me how to make sentences but I am unable to understand the alphabets. :(
First of all you have to define the model - as you're doing - implementing the right interfaces.
Let's say you want to create a user for your application:
public class MyUser : IdentityUser<string, MyUserLogin, MyUserRole, MyUserClaim>
{
public string CompanyName { get; set; }
}
As you can see I've implemented the IdentityUser interface (namespace Microsoft.AspNet.Identity.EntityFramework).
I've specified what type of identifier I want to use for my primary key (string) and included my custom objects to manges login, roles and claims.
Now we can defined the role object:
public class MyRole : IdentityRole<string, MyUserRole>
{
}
Again there's a type and the class I've defined for the management of users belonging to to a role.
public class MyUserRole : IdentityUserRole<string>
{
}
MyUserLogin is going to implement IdentityUserLogin<string>.
MyUserClaim is going to implement IdentityUserClaim<string>.
As you can see each interface need a type for the primary key.
The second step is to create the user store:
public class MyUserStore: UserStore<MyUser, MyRole, string, MyUserLogin, MyUserRole, MyUserClaim>
{
public MyUserStore(MyContext context)
: base(context)
{
}
}
Again we have defined what user, role, login etc etc we want to use.
We need UserStore cause our UserManager is going to need one.
If you're planning to manage roles and associate roles with each user you have to create your RoleStore definition.
public class MyRoleStore : RoleStore<MyRole, string, MyUserRole>
{
public DaufRoleStore(ApplicationDatabaseContext context) : base(context)
{
}
}
Now you can create your UserManager. The UserManager is the real responsible of saving changes to the UserStore.
public class ApplicationUserManager : UserManager<MyUser, string>
{
public ApplicationUserManager(IUserStore<MyUser, string> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new MyUserStore(context.Get<MyContext>()));
manager.UserValidator = new UserValidator<MyUser, string>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
manager.PasswordValidator = new PasswordValidator()
{
RequiredLength = 5,
RequireNonLetterOrDigit = false, // true
// RequireDigit = true,
RequireLowercase = false,
RequireUppercase = false,
};
return (manager);
}
}
This class has a static method which will create a new UserManager for you.
Interesting to note that you can include some validation rules you might need to validate password etc etc.
Last thing is to create or database context.
public class MyContext : IdentityDbContext<MyUser, MyRole, string, MyUserLogin, MyUserRole, MyUserClaim>
{
public MyContext(): base("<your connection string here>")
{
}
public static MyContext Create()
{
return new MyContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<MyUser>()
.ToTable("Users");
modelBuilder.Entity<MyRole>()
.ToTable("Roles");
modelBuilder.Entity<MyUserRole>()
.ToTable("UserRoles");
modelBuilder.Entity<MyUserClaim>()
.ToTable("UserClaims");
modelBuilder.Entity<MyUserLogin>()
.ToTable("UserLogins");
}
}
As you can see I've used the model builder to change the names all the tables.
You can define keys or fields type or tables relations here.
This is the place where you're going to attach your custom classes you want to manage in your context:
public DbSet<MyCustomer> Customers{ get; set; }
Again MyContext has a Create method which returns a new context:
public static MyContext Create()
{
return new MyContext();
}
Now you should have a startup class where you're going to bootstrap your stuff:
[assembly: OwinStartup(typeof(ASPNETIdentity2.Startup))]
namespace ASPNETIdentity2
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.CreatePerOwinContext(MyContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
}
}
}
Here you're going to create your database context and your user manager you can use in your application.
Notice the first line:
[assembly: OwinStartup(typeof(ASPNETIdentity2.Startup))]
This is needed cause you're telling your environment that is the startup class which needs to be called at ... startup.
Now in your controllers you can simply refer to your UserManager doing something like this:
HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
How can you create your tables?
In Visual Studio go to TOOLS -> NuGet Packager Manager -> Package Manager Console.
In the window there's a combobox "Default Project". Choose your ASP.NET MVC project.
Run this command:
Enable-Migrations
It will create a file Configuration.cs in a new folder called Migrations.
If you want to create your database you need to open that file and change the AutomaticMigrationsEnabled to true:
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
Again, from Package Manager Console, you can run:
Update-Database
and all your tables will appear in your database. Don't forget your connection string.
You can download this github project to see how everything works.
You can check these two answers with some other info.
The first of the two has got some links to a blog where you can learn all these things.
NOTE:
You have to do all this if you want to customized every single bit of your environment.

MVC Shared partial view with a dropdown and separate controller

What I am after is having a partial view that hosts a dropdown list of all available languages in the system. This partial view will be used in many edit templates and will be loaded from a separate controller.
Following the articles and information on the net I have the following implementation:
ViewModel
public class LanguagesViewModel
{
public int SelectedID { get; set; }
public virtual SelectList Languages { get; set; }
public LanguagesViewModel(int selectedID, IEnumerable<Language> languages)
{
SelectedID = selectedID;
Languages = new SelectList(languages, "LanguageId", "Name");
}
}
In the Shared folder I have a file: _LanguageDropDownList.cshtml with
#model XNETProductQuote.Web.Models.LanguagesViewModel
#Html.DropDownListFor(model => model.SelectedID, Model.Languages)
I have a LanguageController that has the following implementation
public ActionResult GetAllLanguages()
{
var languages = service.GetAll();
return PartialView("_LanguageDropDownList", new LanguagesViewModel(1, languages));
}
So the above is meant to load the drop down list in that partial view so I can use it in other templates.
From a template that is loaded from a different controller (ApplicationSetting) I call the partial view using:
#Html.Action("GetAllLanguages", "LanguageController")
This doesn't work. It throws an exception:
The controller for path '/ApplicationSetting/Edit/1' was not found or does not implement IController.
What is the correct implementation for such scenario?
Thanks
In Asp.Net MVC when we make a new controller then 'Controller' postfix is automatically attached to the Controller Name for ex:- in your case if you give 'Language' name to the controller then controller's complete name will be like 'LanguageController',so where ever you want to give controller name you have to use only 'Language' not 'LanguageController' and one of the overloads of #Html.Action() is ControllerName which is only 'Language' and not 'LanguageController' ,So in your problem just change #Html.Action("GetAllLanguages", "LanguageController") with #Html.Action("GetAllLanguages", "Language") and your problem will be solved.

Resources