How set session context in SQL for RLS in EF Core - sql-server

We have a web api developed in .Net Core 3.1 which talks to Azure SQL db and running as Azure web app. The database a single database of a multi-tenant app and is protected by row-level security. It requires to set session context before executing any SQL statement. The session context is the primary key of tenant table which is an integer.
I've learned that I can use EF Core Interceptors and set session context. However for security reasons we cannot send/receive tenant id in the URL as a parameter hence we are using another identifier which looks like an encrypted string.
Considering we have a tenant identifier what is the most efficient way to set session context as tenant id? The API is stateless so I can't use session and the controller doesn't require authentication so I don't have a logged in user either. The last option and probably the ugliest way would be to hardcode and maintain a list at server side so that I don't have do a database trip every time.

Considering comments from #JeroenMostert I've decided to do a database trip as it won't be expensive because the data will not going back to client. But after getting some good knowledge and understanding I'll certainly consider using memory optimize table.

Related

Synchronizing identity server 4 instances

I work for a company that owns multiple factories, one of our goals is to allow every factory to continue operating even without a working internet connection. We host a single identity server 4 instance to manage our authentication and authorization. The authentication is done with windows authentication and the domain service is replicated across every factory. In our current setup when the connection to the central identity server is down our users cannot be authenticated.
We want to move to a setup where every factory will host its own version of identity server to authentication can happen even without a connection to the central service. The downside to this approach is that it makes configuration very tedious and error-prone. Most of our users require the same access in every factory, so if we need to update a user's claims or role we need to reconfigure this in every factory. We want to fix this by hosting a central identity server and push all changes to the local instances on a database level. We only want to allow one-way synchronization, so no local configuration or local to central pushing of data.
Are we on the right track with this approach or are we missing something? Are there any best practices for centralized configuration with localized hosting of identity server 4? Any ideas will be helpful.
We want to fix this by hosting a central identity server and push all changes to the local instances on a database level. We only want to allow one-way synchronization, so no local configuration or local to central pushing of data.
Having multi DB and data sync between them seems to be a very good option based on your requirements.
You can have a CI/CD in place for code deployment to make sure all instance apps are sync and move all configurations and user data to DB as your plan.
To sync the data you can use SQL Data Sync for Azure as this is the Distributed Applications case.
Are we on the right track with this approach or are we missing something? Are there any best practices for centralized configuration with localized hosting of identity server 4? Any ideas will be helpful.
I'm not sure if this is much related to IdentityServer, this sounds more like a distributed application solution to me. Saying this as you might need to extend your search criteria to look for any application type with same model.

.NET Core SQL Server Session State

I need to use SQL Server session state with my website and use it with the existing ASPState database that the customer has, this was a requirement.
However I noticed the table structure for doing session state in .NET Core differs from how the structure used to be. Does anyone know how to implement this or a work around?
.NET Core Session State DB
Previous Session State Versions
What you're wanting is not possible. Not only is the database table different, ASP.NET Core uses DataProtectionProvider for encryption, whereas ASP.NET utilizes the machine key. In other words, even if the schema was the same, one site wouldn't be able to decrypt the session state from the other.

Security issues with allowing anonymous users to create SQL Server login and accounts?

I have a rich client program installed on users PCs where I want to start storing some user created data on SQL Azure/SQL Server. The potential anonymous-to-me users would key in their name, email account and a password which would get stored on SQL Azure/SQL Server. Then they would start generating their own data. I'm anticipating volumes of maybe 1000 users.
There are times when those users would like to run their own queries against their own data but, obviously, I must ensure that they can never view other users data.
I'm thinking the best way to ensure security of data is for each user to be issued their own SQL Azure account and password. I will setup a SQL Azure user and long password, known only to me, which only has permissions to execute several stored procedures with appropriate parameters being passed to those SPs which will create the SQL Server accounts, logins and add the users to a role which I have created.
Obviously someone running debugging tools could figure out the user name and password but I'm thinking this isn't a big deal. If all that particular SQL Azure account can do is execute a few SPs so what if a malicious individual starts doing that. I will only allow a very limited amount of data to be uploaded before I require payment.
The users can only insert records using stored procedures which use the following:
SELECT #uName=SYSTEM_USER
and only select appropriate parent records. All stored procedures which users can execute would have the above as required to ensure they can only work with their own records.
All views will have embedded with them WHERE clauses such as
WHERE tbLoginName = SYSTEM_USER.
I'm new to SQL Server so I may be missing some fundamental concepts so I'd appreciate any and all comments.
The issue is, as pointed out on http://msdn.microsoft.com/en-us/library/ms189751.aspx:
In SQL Azure, only the server-level principal login (created by the provisioning process) or members of the loginmanager database role in the master database can create new logins.
Those accounts are also capable of alter and drop logins. So if you embed those accounts in the client application, you’re essentially granting every user permission to alter/drop other users accounts. While an average user won’t do that, a hacker will. So you cannot let a client application manage SQL Azure logins, unless only trusted users (such as your IT administrator) are permitted to use the app.
Best Regards,
Ming Xu.
I would like to point out a potential issue in the approach you mentioned: Your master SQL Azure account need to have privilege to create new accounts and grant them access to particular tables. This means your master account itself need to also have access to all those tables. If you store the master account on the client side, a clever user will get access to all users data.
From my experience, connecting to a database directly from a client side application will almost always make your solution less secure. You can do that for testing purposes, but in a real world solution, I would like to suggest you to use a service. You can host the service in Windows Azure. Then the service will access the database, and client application can only access the service. In this way, you can authenticate clients using any mechanisms you like, such as ASP.NET membership.
Best Regards,
Ming Xu.
You are essentially creating a physical two-tier database connection, allowing a client application to connect directly to the database. This is a problem in many ways, including security and performance. From a security standpoint, not controlling from where your customers will connect, you will need to keep your firewall rule wide open for anyone in the world to try to hack every customer uid/pwd. And instead of having only 1 user id to play with, hackers will have up to 1,000...
The second issue is performance. You applications will be unable to leverage connection pooling, creating undue stress on your database server and possibly hitting throttling issues at some point. Using a web service, with ASP.NET membership to manage logins, and using a service account (i.e. the same uid/pwd) to get data will ensure you will leverage connection pooling correctly if you keep the connection string the same for all your requests.
With a web service layer you also have a whole slew of new options at your fingertips that a two-tier architecture can't offer. This includes centralizing your business and data access logic, adding caching for improved performance, adding auditing in a centralized location, allowing to make updates to parts of your applications without redeploying anything at your customer locations and so much more...
In the cloud, you are much better off leveraging web services.
My 2 cents.

Using SQL Server Users and Roles as an authorization database for an intranet web application?

I have a question that really feels like I should have an easy answer to, but for one reason or another I haven't been able to totally reason around it.
I'm embarking on development of an ASP.NET MVC3 intranet application, and I'm currently working on designing authentication & authorization. We're forced to use basic authentication in our environment, and we use Active Directory, so the authorization part is generally taken care of. Unfortunately our role/user hierarchy in active directory doesn't mirror what I need for the roles in the application, so I'm going to have to define my own.
I'm using SQL Server, so I was originally thinking of using stored procedures for all DML, and then creating roles and adding users in roles in SQL Server, and then controlling access to the stored procedures via those roles. I was also thinking I could query for those SQL Server database-level users & roles in order to use that as the source of authorization info in the application itself. That originally seemed like a great idea, but it doesn't seem like a popular one (for one, it seems the queries for that are a little long and messy for what they produce). Alternatively, would it be better to have the web app impersonate a user for all queries to the server, and then implement a user/role database with my own schema, and only authorize on the application side?
It originally seemed that authorizing on both the application and database side would be a good thing for security, and using the SQL Server user/role objects means that the user and role data wouldn't need to be stored in two places.
I did see some potentially relevant discussion at Best practice on users/roles on SQL Server for a web application, but I think this is a different question overall.
Thanks!
I recommend creating a sql login that the web application will use to connect to sql server. This way you are not impersonating any specific AD account which may get deleted, disabled in the future and can control the user strickly in SQL Server.
I would then recommend implementing roles based authentication in your application. This will enable you to create users and roles that are custom to your application and then assign users to them. This way if a user tries to access a resource that their role is not allowed it will not do any work. Here is a demo app based on this principle http://www.codeproject.com/KB/web-security/rolesbasedauthentication.aspx.

What are the best practices on MS-SQL when Windows Authentications is not an option?

What is the best option for a windows application that uses SQL server authentication? Should I create a single SQL account and manage the users inside the application (using a users table). Or should I create a SQL server account for each user. What is your experience? Thank you!
Depends on whether the username/password for the SQL server would be exposed to the user, and whether that would be a problem. Generally for internal apps (in smaller organisations), one would trust the users not too log in directly to the sql server. If you have a middleware layer (ie webservices) the password can be hidden from user.
I prefer to use a general login for the DB and manage users in the application. Even if you do create a login to sql for each application user, they could still connect directly, so why not just use a generic sql login that is easier to manage. This is of course assuming all users have the same accesses.
One good practice, if the users potentially can get direct access to the db, would be to grant access only through Stored Procedures and not directly to tables, so that only certain actions can be performed. Steer away from writing business logic or security checks (except basic ones) within the stored procs.
One way I would solve your problem is to write some webservices that check security and does your CRUD (via datasets, etc), but again it depends on the app and environment.
In summary if you have a middle layer or all users have the same access manage the user within the application and use a single user login. Otherwise use a login per user or role.
One option that I have used in the past is to use the ASP.NET Membership Provider. It makes authentication a breeze to use. The only drawback that I saw was that it added a bunch of tables to your database.
The code for using it is very straight-forward.
Here's a blog post about using this in a Windows app. http://msmvps.com/blogs/theproblemsolver/archive/2006/01/12/80905.aspx Here's another article with more details. http://www.c-sharpcorner.com/UploadFile/jmcfet/Provider-basedASP.NET10162006104542AM/Provider-basedASP.NET.aspx
Here's another article that talks about using it with Windows applications: http://www.theproblemsolver.nl/usingthemembershipproviderinwinforms.htm
Google for "ASP.NET 2.0 Membership Provider", and you will get plenty of hits.
What about having SQL accounts based on the level of permissions needed for the task. For example you could have a read only account just used for reporting if your system has a lot of reporting. You would also need an account what has write access for people to change their passwords and other user admin tasks.
If you have situations where certain users are only going to have access to certain data I would have separate accounts for that data. The problem with using 1 account is you are saying that there is no SQL injection anywhere in your application. That is something everyone would strive for but sometimes perfect security is not possible, hence the multi-pronged approach.

Resources