I created a login system for one MVC App based on the new Identity framework, and since I already went through all the hard work of modifying my database to match the Identity 2.0 requirements, I was wondering if it would be possible to use it outside of MVC, so I could reuse what I already created, like a login system for a desktop project that I'm working on for example. If so, can I implement the login system on a .dll that can be reused on other projects?
Yes, you could use your existing database for another application. Add the ADO.NET Entity Data Model, and point it to your database. Then, if you need to, select "Update Model from Database" and you should be all set.
Although, it may be just as easy to create your own user/roles tables. It's frustrating (to me) that Identity creates the Primary keys as strings, even though they are essentially Guids.
Related
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.)
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.
My title is a bit complex, but I will break down really simply what I am doing here.
I have an existing database with about 20 tables. I have a project that my partner has decided that we should use the old Simple Membership over Identity. This is no longer an option on the table, we have finalized that decision. WE will be creating the project in Web Forms using Visual Studio 2012 because that is what we have licenses for.
Step 1) I started a new project in VS 2012 using Web Forms and I pointed the Default Connection String at my current database and registered a few users. This created the tables I needed for membership in my SQL Server 2012 database.
Step 2) I did some relational mapping in my database connecting existing tables to the new membership UserID's where needed so that my existing tables are now related to the membership stuff.
Step 3) Here is where I am a bit lost because normally I have used SqlDataSources with Web Forms in the past. I have some experience with MVC so I would like to do things differently in this web forms project and start from Database First and import my database tables into a Data Access Layer, I'm thinking of using a Class Library for this.
MY QUESTION: When importing my EDMX for database first. Should I
A) Bring in all of the tables (Existing Tables + New Membership Tables)
B) Just bring in the Tables that are NOT part of Membership?
C) Bring in my Existing Tables and only the User and Membership table? or
D) Do you have any other idea?
My reasoning for this question is that I know that I can access the Membership stuff in the Web Forms Project but will it possibly be needed in the Class Library since my application will eventually be very User centric. Lots of tracking of user time and logins, assigning tasks and projects, permissions to that user etc.. I was leaning toward (A)
I know this is a very complex question or maybe I should say it may have many answers all due to things that I may not know yet or I have not explained fully. But I'm looking for some direction as I have never used a Class Library or Database first in this way or in a Web Forms Application.
Snapshot of Database:
You really don't need to include the Membership tables in the EF model. As you pointed out, the Membership class will handle it as long as you have it properly configured in the Web.Config file.
You should import only those tables that are related to your application.
You would only import the membership tables only if you are building a custom membership provider, which is old news by now.
At least, I personally don't do it. This also helps to keep the EF model with just what I need.
Hope it helps.
My understanding is that a context connects to a database via a connection string specified in web.config. When I create new scaffolds it creates new database .mdf files with a new connection string. I am coming from rails active record with one database and one connection string. So why does asp.net do it differently? Why have multiple database files?
Also I changed the connection string for the user model to my default site database called Toolsite to simplify things and put the users in the same db as everything else -
public ApplicationDbContext()
: base("Toolsite")
{
System.Diagnostics.Debug.Write(Database.Connection.ConnectionString);
}
and this caused a Invalid object name 'dbo.AspNetUsers'. exception even though the table AspNetUsers exists in the database "ToolSite". Is there any other part in the server code that needs to change?
The default MVC project scaffold has always been a pet peeve of mine. When you create a new MVC project with auth, you get a user model and an application context to wire it up. That's great, in one respect, because it lets you immediately run the project with no extra configuration. However, they stupidly decided to put the user model and the application context in the same .cs file, so it confuses developers not familiar with the setup into thinking that the two are intrinsically linked.
In reality, MVC applications are designed to have a single context, and you should actually add your models to the existing context created by the scaffold, rather than create a new, separate context. The only reason to have multiple contexts is if you're literally dealing with multiple databases, and then, only really because you're dealing with existing databases in addition to your application-specific database. In other words, you should only have a single context that is tied to migrations and then you may or may not have additional contexts that operate on a database-first basis. Once you understand that, it becomes far less confusing to work with a scaffolded project.
I am looking at migrating an old app to MVC4 but can't yet modify the database. The existing database already has it's own user / group / membership tables setup (in a rather quirky way might I add, but it works).
With the MVC3 style MembershipProviders I could roll my own extending what I needed to login, check role permissions, modify user attributes etc. but I'm at a loss as to how to achieve this with MVC4 and it's SimpleMembership and then how to use OpenAuth alongside it as an alternate way to login.
I've been looking around but there seems to be very little content online yet regarding it, any ideas?
I don't require a SimpleMembership implementation, if anyone is aware of another similar provider that would do too.
SimpleMembership is just like its name: simple. So if you want to extend the membership provider to meet your complex requirement of roles and membership, it's not a good choice. If you do really have to upgrade your app to mvc4, just do it, then reuse the ASP.Net Membership that exited in your app. It's still running properly on mvc4.
One thing to try is to just use SimpleMembership and add as many roles as you need to make up for your current "attributes". Depends on what you mean by "attributes". If you just want to add additional columns to the UsersProfile table, it's pretty easy to do. And then the SimpleMembership API can access those new columns just as it does the built-in ones in a default MVC4 Internet App. project.
Have you viewed this SO thread?