How to connect my already constructed MySQL database to my Blazor app as an Identity DB Context!?.
hello guys, I need help to connect my MySQL database to a new blazor app as an identity db context, I followed a lot of tutorials but all fails, I am new to db migration , tried it and it just created new tables to my database, I already has the database and tables, I just want my blazor app to connect to mysql database and use the data in it for signup/sign in, how to activate the register/login options and connect my database to my app?
my database already works as an Identity db context on another .NET framwork app with no issues.
40 Replies
Is this a Blazor Server app or WASM?
Server app, I am creating an Admin dashboard for the database, db already workign as an Identity db context for the users' app using .net framework, the web app is quite old, I fix bugs, clean code and add features, but I have no idea how the devs before me even added the db as identity.
Well for connecting the database you need to provide the connection string somewhere and then you have to build the pages and functionality to login and register
And maybe this here might also help:
https://medium.com/informatics/blazor-server-project-12-installing-asp-net-core-identity-50413dabd55a
Medium
Blazor Server Project #12: Installing ASP.NET Core Identity
An initial guide to security: installing Identity and adding its database to the existing project
is it possible to do that manually with .Net 6 and blazor?
I see a lot of built in functions/interfaces used in the web app that base on using add-migration / update-database commands and UserManager Class.
Also when I tried using migration it just added new empty tables to my database, glad I am using a dev environment 😅
migrations and updating the database has nothing to do with Identity itself or logging users in
it's just the way you manage db changes with code first
so I dont need to migrate/update db ? just using dbContext and assign it as Identity from base code?
I can skip the migration part?
Well you need to migrate and update if you change your entities in the code
and of course you need somewhere a migration that adds the tables for Identity
what if I already has the tables for identity? old app using them, cant I just use the same tables /database for a different app?
that depends if you want those users to be able to access your app or not
But then you probably also have two databases, one for your application data, the other one for the user information
I will totally give this a read and try to apply, thanks 🙂
also a problem can be, if someone extended the user information there or roles
and you don't want or need that, you have a problem
because you can't change it just for your app
so I would be very careful with this approach just using the existing database
so, I need another whole database and after creating an Identity I just connect the other db for example to manage users data from admin side etc?
What other db?
for admin dashbord
If you create a new one, your application data and Identity should be stored in the same db
just for identity/ login
yes exactly, they are suppose to be in same db, no need for new one, then whats the uses of migration? sorry but I am totally new to this concept
I don't know what you're talking about
you are confusing so many things
okay my bad, I will try and clarify slowly
Also MySQL as DB is a pretty bad choice
Well, I cant change the DB since its not my choice, I am just a developer in a company that uses it
would have been much much easier if they used sql server since its built in with blazor and visual studio
That wouldn't change anything in your scenario
yeah I agree
you still need to provide a connection string to tell the application where the DB is located, the DB itself is not that important for that
I can do that, but how to make it as identity? shall I manually code the claims,users,roles etc related to identity dB context?
I don't think you understand how Identity works at all
When you add Identity to your project correctly and then create the migration and update the database you have tables for users, roles already
well not totally, I know that It's for authorization and authentication
and then you can decide if you need to extend them or not
yes I have them already, both tables exists
so then also the connection string is in place, otherwise they couldn't have been created
but when I added the connectionstring to my databtase on program.cs and followed the tutorial a=I was watching,
used add-migration then update-database, it totally added new tables for example:
aspcoreusers
aspcoreroles
I don't know which tutorial you watched and what exactly you did there
ZetBit
YouTube
How to use Identity in ASP.NET Core with MySQL (Part 1 of 2)
In this video i will show you how to use identity in ASP.NET Core and how to set it up with MySQL instead of MSSQL.
Identity is a powerful framework to use designed by Microsoft for allowing users to be added to your website.
Download Project: https://zetbit.tech/Downloads/Project/1/Identity-Project-Part-1-and-2
00:00 Introduction
01:24 Scaffol...
this is the tutorial I followed
But this isn't even a Blazor Server Project
But yeah the Identity parts shouldn't be much different, you scaffold them and then apply the migration to your database
after that you can start building pages e.g. for login, etc. or use the existing ones and customize them
that's on you and your requirements
But Identity pretty much gives you everything you need for the basics
yeah since I am using asp .NET 6 , it shouldnt be any different or atl east not much difference
btw the link you provided is locked behind upgrading to premium, I cant read it.
Just using the incognito mode should make it work
I cant in both ways, wbsite wants me to login, after logging in wants me to pay to read the full article
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.