Linq to entities connection string depending on user role - asp.net

I am having a real problem, I created multiple users in sql server with different dbroles, and now im trying to check what's the user role in db and connect to a connection string depending on their role as this will be more secure..... how can I choose between different connection strings and pass it to the model.edmx, remember I am working with 3 tier design.
here is how my login control works:
http://i40.tinypic.com/nvz6lt.jpg
here is my connectionclass:
http://i39.tinypic.com/34qmybs.jpg
here is my app.config file:
http://i43.tinypic.com/6xq4q8.jpg
Thanks alot

There are multiple overloads for the ObjectContext constructor.
The default one just takes the connectionstring with a matching name from the config file. But you can also use a constructor where you specify the connection string yourself.
In your code you could maybe create a Authorization enum with values like BasicUser, Admin and pas that to your ConnectionClass. There you could do a switch and pick the right connection string from your config.

Related

Get application name that executes specific stored procedure

I want to know the application name that executes a specific stored procedure. We have many applications and all have application name property inside the connection string.
Here I don't exactly know which application calling that stored procedure. I think we can get this by calling APP_NAME() but I don't know the exact query to get the correct application name.
SELECT APP_NAME()
The APP_NAME() is a built-in function to return the 'Application Name' token from the connection string used to set up the current session.
Here is a good article on setting up your connection string appropriately:
http://www.sqlerudition.com/using-a-meaningful-application-name-in-the-oledb-connection-string/
If you don't have control over the connection string, then you will need to use a combination of other session variables such as SESSION_USER() or ##SPID and possibly refer to the dmv called sys.dm_exec_sessions for a full record of what the server 'knows' about the connection.
https://learn.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-exec-sessions-transact-sql?view=sql-server-2017
If you want detailed information, i would suggest to use Extended Events by selecting rpc_completed event. For detailed steps..

How to check which IIS application is calling the database

I have 2 application (websites) on one IIS and which is calling same remote database. Is there any thing in SQL server which will give me in stored procedure that who is calling :) ?
APP_NAME() returns the application name for the current session if set by the application. An another way of identifying the app is to log the app name through the CRUD operation , so that an additional field will be added in the tables to hold the app name.
You need to modify your connection strings and add Application Name
Data Source=myServer;
Initial Catalog=myDB;
User Id=myUsername;
Password=myPassword;
Application Name=myApp;
Now when you query sys.processes,program name will be application name
References:
http://johnnycoder.com/blog/2006/10/24/take-advantage-of-application-name/
https://www.connectionstrings.com/sql-server/
Check this thread if you are using VB.NET:
How to set "Application Name" in ADODB connection string
if not above will work

Is there any way to get number of invalid attempt of password from membership api

I need to check how many invalid attempt a user taken at the time of log in.
This is becauase our requirement is to save the max attempt password value from db not config.
The default Membership API doesn't expose the invalid attempt counts, but they are indeed tracked in the database.
If you take a look at the documentation for the Sample Membership Provider Implementation you'll see the following in the Database schema section:
FailedPasswordAttemptCount Integer,
FailedPasswordAttemptWindowStart DateTime,
FailedPasswordAnswerAttemptCount Integer,
FailedPasswordAnswerAttemptWindowStart DateTime
These work in conjunction with the PasswordAttemptWindow setting to lock users out if they fail to supply the correct values, and the counts are updated by the default provider when the user fails to log in.
If you want to manage these through a database rather than the web.config you only really have one choice: Write a custom membership provider (based on the sample) that reads the required values from the DB rather than the config settings.
This is because the property on the provider is read-only so you can't modify it once the provider is loaded and instantiated.

What is the Default Database for Entities Code-First without defined Connection String?

I have written a trivial Entities code-first WinForms application with one simple class and one database context class as all the tuturials describe it.
But I did not add a connection string in the app.config file.
Nevertheless, when I start the application, it can insert objects into the database and even show all objects already inserted.
I figure there must be some default database in SQL Server for that case but I cannot find out which instance and database name is used.
Database name is the same as the name on your DbContext class. By default it creates a database on the local machine Sql Server express installation. You should be able to see it in Sql Server Managment when you connect to your local SQLEXPRESS.
If you spesify a connectionstring with the same name as youd DbContext it will use that instead.

Is it possible to change an ASP.NET Web.Config value at runtime without reloading the application domain?

Is it possible to change a database connection string value in the Web.Config at runtime without reloading the application domain? The reason for doing this is that:
I am building a multi-tenanted application which uses the one code instance and multiple databases instances approach, so the database connection string in the web.config must be able to be changed at runtime for different tenant users.
ADO.NET always injecst the database connection string in the web.config when you are using entity framework/LINQ to SQL etc, dataset etc.
So I may have to do something weird like this. Hope it is clear enough. Thanks a lot!
Yes it is possible.
try this
System.Configuration.Configuration conf = WebConfigurationManager.OpenWebConfiguration(Server.MapPath);
conf.ConnectionStrings.ConnectionStrings["comp1"].ConnectionString = _connection_comp1;
conf.ConnectionStrings.ConnectionStrings["comp2"].ConnectionString = _connection_comp2;
conf.AppSettings.Settings["CompanyCode"].Value = _company_code;
conf.Save();
Use configuration sections.
http://www.kodyaz.com/articles/add-connection-string-to-web-configuration-file.aspx

Resources