Implement custom ASP.NET Identity using existing database - asp.net

I would like to customize ASP.NET Identity to work with an external database.
The default schema looks like that:
The part that bothers me, is the User table.
I succeeded to move the database to SQL Server Express and then I tried to modify the Table and deleted some columns.
When I tried to build and run the project I got multiple errors demanding for the columns I deleted and there is no mention to them in the code (Just new MVC project).
I would like that it will look more like this:
Of course I'll want to add some more table but that we'll be after the Users part will be complete.
So, is there some kind of tutorial that can explain how to customize Identity to my needs ?
Or anything help that can help me build stable users system.

Related

Copy some entries from one server database to other server database using asp.net mvc

We are doing some CRUD operation in DEV environment and data is saved in database. For other environments(like Staging/Prod) we want to copy those records from DEV database and paste to Staging/Prod when required using asp.net MVC. Is it possible? Could you please suggest some pathway in order to accomplish this?
You can accomplish this using a linked server:
https://learn.microsoft.com/en-us/sql/relational-databases/linked-servers/create-linked-servers-sql-server-database-engine?view=sql-server-ver15
This involves creating a link between one database server and another (they must be able to see each other on the network). You can then reference the linked server like this:
INSERT INTO [LinkedServerName].[Database].[dbo].[Table] (......
Alternatively you could use a paid for tool like SSMS tools which can generate insert statements on a per table basis: https://www.ssmstoolspack.com/

Different user for migrations

I'm trying to run
dotnet ef database update
When I do so, I get an error about not being allowed to CREATE TABLE. Not entirely surprising as I don't want the user I have the website running under to be able to create tables. So, after a bit of searching I found a solution that basically created an inherited context, and with that context used a different set of credentials. So, I tried;
dotnet ef database update --context ScaffoldContext
And I got the same error. I checked my connection string, yes, it's a user I can use to create a table with. Confirmed through SQL CLI. So, I added CREATE TABLE privileges to my site user, and the error changed. Suggesting that the base connection string was the one that mattered and it's ignoring using my elevated user. I tried moving the configuration into the OnConfiguring override in my inherited scaffold context, instead of services.AddDbContext in my Startup.cs. However, looking this up it looks like the wrong way to go about that. When I added CREATE TABLE privilege to my site user, I got a different exception about not being allowed to touch dbo.
This is driving me nuts, I don't want to use my site user as my migration user and it seems every example I find is from older versions of EF or dotnet core. Does anybody have any solid guides on how to go about managing users correctly using migrations with 2.1?
Note: If I change my connection string to be my sa user, it works fine. So the migration will go through. I'm just not wanting to give either full privileges to my site user or swapping credentials around in connection strings every time I need to run a migration.

Is it possible?

We are thinking of moving some of the 'hard coded' settings out of our windows forms application and storing them in the sql database in a table called App_Settings. The reason for this is we have values currently hard coded into appsettings and app.config which can change over time, and it is much easier and faster to update the values in a database table than it is to update, build and deploy the app over three servers.
Please can someone advise on this, and also how can we load the settings into the app and then have them readily available in any class?
Cheers
Richard
Have a look at this similar thread: Resources/App.config or Database where is the best place to application strings
When you want to store settings in the database i would implement the class as Singleton or at least as a Class with only static/shared members and a factory method(getAppSettings)which returns that single/static instance. So you could access your settings from everywhere and it is only initialized once.
Why don't you use something like YAML to save these settings? It would be easy to create a form to edit and save these settings in a file, instead of a db (which would add more maintainance problems).
Otherwise, I would suggest to use something like NHibernate to use a database, and SQLLite as the db server.
Also, note that if you are updating these settings, I would not say that these are App_Settings, since App_Settings aren't usually modifyable without
Help this helps,
Pietro

Is there a way to rebuild/repair ASP.Net personalization tables without losing the data inside?

I inherited an Asp.Net app that uses ASP.Net membership services. I am trying to add web parts with personalization to the site, and am getting a lot of errors. It looks like the tables generated by Aspnet_regsql.exe have been changed - probably copied at some point in the past using "select into" causing them to lose all their indexes and primary keys.
How can I repair these tables without losing all the data inside?
Backup database (just in case!)
Rename existing database
Recreate aspnetdb
Bulk copy data from renamed DB into newly recreated DB. (probably with BCP)
I know BCP is an old tool... but it still works. Maybe there is an easier way to do it, but this is how I would do it.

How to create a database and populate it during setup

I would like to find a way to create and populate a database during asp.net setup.
So, what I'm willing to do is:
Create the database during the setup
Populate the database with some initial data (country codes or something like that)
Create the appropriate connection string in the configuration file
I'm using .NET 3.5 and Visual Studio 2005, and the Database is SQL Server 2005.
Thanks in advance.
If you are creating an installer I'm sure there is a way to do it in there, but I am not all that familiar with that.
Otherwise, what you might do is the following.
Add a application_start handler in the Global.asax, check for valid connection string, if it doesn't exist, continue to step two.
Login to the server using a default connection string
Execute the needed scripts to create the database and objects needed.
Update the web.config with the connection information
The key here is determining what the "default" connection string is. Possibly a second configuration value.
Generally, you'll need to have SQL scripts to do this. I tend to do this anyway, as it makes maintaining and versioning the database much easier in the long run.
The core idea is, upon running the setup program, you'll have a custom action to execute this script. The user executing your setup will need permissions to:
Create a database
Create tables and other database-level objects in the newly-created database
Populate data
Your scripts will take care of all of that, though. You'll have a CREATE DATABASE command, the appropriate CREATE SCHEMA, CREATE TABLE, CREATE VIEW, etc. commands, and then after the schema is built, the appropriate INSERT statements to populate the data.
I normally break this into multiple scripts, but YMMV:
Create schema script
"Common scripts" (one for the equivalent of aspnet_regsql for web projects, one with the creation of the Enterprise Library logging tables and procs)
Create stored procedure script, if necessary (to be executed after the schema's created)
Populate initial data script
For future maintenance, I create upgrade scripts where a single script typically handles the entire upgrade process.
When writing the scripts, be sure to use the appropriate safety checks (IF EXISTS, etc) before creating objects. And I tend to make mine transactional, as well.
Good luck!
Well, actually I found a tutorial on MSDN: Walkthrough: Using a Custom Action to Create a Database at Installation
I'll use that and see how it goes, thanks for your help guys, I'll let you know how it goes.
If you can use Linq to Sql then this is easy.
Just import your entire database into the Linq to Sql designer. This will create objects that describe all objects in your database, including the System.Data.Linq.DataContext derived class that encapsulate the entire database setup.
Now you can call DataContext.CreateDatabase() to create the database.
See here more information.

Resources