asp.net web api, Combine EntityFramework generated models with ApplicationUser model - c#

Hi im a beginner in the use of asp.net framework and web development.
So im leaerning by starting a project.
Im using asp.net web api. Im having issues understanding how to use my
own entity framework codefirst database generated models, with the AppUserModel found in the
IdentityModel.cs.
The thing is that i have a class library where i defined a model
Staff. It contains properties like ID Username, password, Name,
and StaffCategory. For the staffCategory this is defined in another
file as an enum. Where the keys are Employee and Manager.
THis model is mapped to my existing database.
From the way i designed this model, i had intended to use it as my user account.
Also then in my code i intended to add roles EmployeeRole and ManagerRole corresponsng to StaffCategory.
But now on creating the asp.net web application i see that there is a user model already.
ApplicationUser. And is used for user account creation and authentication.
Im really confused now about how my model Staff fits in right now.
My questions are
1. Can i use my own model(Staff) for user account? If so is it best practice,
and how can i achieve this? And how do i mix roles with it?
2.If i cannot use my Staff model for user accounts. How do i use the ApplicationUser Model
together with my Staff model?
Sorry if my question is bulky. But i have pondered and researched for so long without staright answers to
these questions. So i just need to find out.
Ohh and example code for this would be really helpful for better understanding.
Thanks in advance.

Related

ASP.NET MVC Individual user accounts configuration

I want to create an application that uses individual user accounts identity functionality of MVC. I also want to harness this functionality in order to extend the application so I can add new fields to the register page so that it populates not only the AspNetUsers Table, but also to a custom table that I had made locally within Visual Studio called Customers. I've set up the table so that the customer table has a foreign key field for the GUID that is produced when a ASPNetUser is created. So far, ive found it challenging as I dont really know where to start and find other tutorials I found not really answering my question.
Does anyone have any tips? is this going to be harder than starting an MVC application and then adding in identity functionality afterwards in order to achieve this goal?
Thanks

ASP.NET MVC UML class diagram design

This is my first attempt in creating an MVC program and my first month using ASP.NET.
So far I understand the basic from MVC framework. The software that I am trying to create is an online application, basically a survey in which the admin is able to modify the questions and members with a login are able to see the results from the survey.
I am currently stuck trying to figure out how my class diagram will look like.
So far the Model interacts with the database and contains the following classes
DAOSurvey (get and setter for the data)
SurveyModel (Execute all the queries)
SurveyQueries (Add and retrieve the results from the survey)
ISurvey (Interface to communicate with controller)
Now, I know that I need some view classes, it is correct to have a "Loginview" "Adminview" "Membersview" and "Surveyview" ? I should create a class for every asp page in my ASP.NET application?
Finally I need the controller classes, which I am guessing that Admin and Members should be.
I am quite sure about the model side of my class diagram should look like, however, I am lost about how view classes and controller classes should be. Any suggestions?.
Thanks in advance.
In ASP.NET MVC you will get features like Login and register automatically if you change the authentication to Individual user account while creating a new project or you can manually change it if you have already created the project.
Here you can create logins for different user roles and then use role based authorization for different controllers.

Implementing asp.net Identity into already existing database with entity framework

I am trying to update an older website. It already has an existing database with a user and a role table, with existing data in it.
I need to create a new web-api and a new web project and integrate ASP.NET Identity into the already existing database.
I have currently setup my solution to breakup the projects as follows:
Domain
Services
WEB_API
WEB_UI
My aim is to implement the Identity in the services layer so that both my WebAPI and MVC site can utilize the same identity mechanism.
I am fairly overwhelmed at the moment, I have read many tutorials and articles over the last day or two and have ended up with the options of either re-implementing the IUserStore and IRoleStore. And also with mapping the different entities in the OnModelCreatingMethod.
I cant seem to find a tutorial which is aimed at what I want to do. My database is to different to simply remap the names of columns, And I dont want to re-implement the entire Identity Stores as there are only a few conflicting fields. Most tuts I have found are related to using mysql instead of EF. I still want to make use Entity Framework.
Issues I have:
The current Users Table in the database used Int pk , not GUID
The Password Field uses a different Hashing algorithm. So i would need to override how Identity checks and store the password.
I do not have all the required Identity User fields in my database, however I am able to add new fields, I just cant change already existing fields.
I am making use of Database First as the DB already exists.
So basically my question is, In what direction do I need to go in order to overcome the above mentioned issues. Can I get away with changing the Database mapping? Or do I need to go as far as re implementing the User and Role Stores?
What I had initially planned was to re-implement the User and Role Stores using entity framework, And i could then make use if the DB first model classes and map the actual DB structure and fields to my ApplicationUser Fields. But this is where I thought I might be diving into cold waters, and i'm generally not a fan of reinventing the wheel if not necessary.
-The first thing you should do if you haven't already is to BACK-UP your current database!
You could use code first to update an existing database.
I found this walkthrough and it seemed close to the route you're on, and I'm hoping it will help solve your problem, begin at "Migrating to Visual Studio 2013".
http://www.asp.net/identity/overview/migrations/migrating-an-existing-website-from-sql-membership-to-aspnet-identity
This would create a few new tables, but not necessarily a new database.
Download Nuget packages:
Microsoft.AspNet.Identity.EntityFramework,
Microsoft.AspNet.Identity.Owin,
Microsoft.Owin.Host.SystemWeb
Enable-migrations and run a script such as this: https://aspnet.codeplex.com/SourceControl/latest#Samples/Identity/SQLMembership-Identity-OWIN/Migrations.sql
Or you could try using a reverse POCO generator found in visual studio extensions.
(My 1st attempt at writing an answer here. Tried to clarify.)

Using own employee table to manage users in mvc using roles and identity

I am currently new to ASP .Net MVC and I'm working on a project. I already created my database which has a Employees table (which will consist of the users of the system) where I would want to manage all employees. I know mvc comes with a built in functionality that creates the roles and users table for you from code migration. How would I be able to implement that with my own database? Another problem, I tried creating a default and script the tables, added them to my database but the primary keys which is of type int doesn't match that of the id in the dbo.aspnetusers table that was created.
Can anyone assist me?
Regards
Your problem is bit unclear. but from what i have understood so far i can say that if your application is to be used internally, then you should consider using windows authentication and implement your own role provider.
Additionally you will need to provide more detail on what your are trying to achieve. this sounds to me like more of a design question rather than implementation question.

How do I do a registration in ASP.NET MVC?

I'm relatively new to ASP.NET MVC, but I already did C# for a while and I already took the MVC basic course. What I'm stuck with is the "ASP.NET Membership".
For my website, I need a registration (with email, password, and some cstom fields), a login, "roles" (user, admin. moderator, etc...), "settings" (change password, etc ...) and cca. 40 custom fields (like date of birth, favorite color, car type, etc ...) connected to the user.
How should I make it in MVC?
I have found the SimpleMembership, but I don't need most of it's features, and the features I need are missing.
My time to shine!
I would recommend using the Entity Framework, because reasons. It makes DB interactions very simple and readable.
you can write your own membership provider in MVC, which like you said you wont need most of the features, so you dont need to implement them. This dude implemented it all here ->
http://www.brianlegg.com/post/2011/05/09/Implementing-your-own-RoleProvider-and-MembershipProvider-in-MVC-3.aspx
He tells which ones to focus on, and shows roles and authorization.
Essentially you will map a User model to your User table using the entity framework, then use that user model when the person is registering. You can even use validation attributes, as seen here->
http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validation-with-the-data-annotation-validators-cs
What other questions may I answer to help you on your journey today my good sir?
Personally, I like to use the built-in membership provider (have yet to look at the SimpleMembership) for the registration, login and roles.
I then simply create my own member tables that I tie together with the membership User table.
This simple approach prevents me from having to create my own membership provider, and I am free to define the member table the way I want.

Categories

Resources