Two ASP.NET websites that shares one database - c#

In general I need to know amount of visits on my website and access that data via API to have it everywhere.
For this I am trying to share EF database with 2 projects. One is simple Azure ASP.NET website with one controller which collects statistics of site visits. Second project is Azure mobile service that connects to the same database as website and provides access to that statistic via GET requests.
Locally I am getting such error:
Cannot attach file '...App_Data\aspnet-TargetrWebsite-20151001100420.mdf' as database 'aspnet-TargetrWebsite-20151001100420' because this database name is already attached with file '...\tagetr_statisticService\App_Data
So the problem that I have 2 web.config files with connection strings that points for 2 different files with the same database name.
How to get this work with one file on localhost and keep it worked on production as well?

Actually my target is know visits of page from everywhere. It is not required to use separated service for this. Just adding new authenticated controller which binds to Visits table on the same website solves the problem. Service removed then.

This could probably be done via Powershell script which sits on any machine.
Here's a good start where you can get back a list of IP addresses which are stored in an xml. You can then pull the xml into API quite easily I would believe. Also it should be quite easy to convert IP to url or location etc.
https://www.petri.com/powershell-problem-solver - Thanks to Jeff
Remember to watch your permissions!

Related

Versioning issue when updating databases, api and web application

I have thousand of SQL Server databases (one for each client). When we decide to push on production, we have most of the time changes in databases, the web API and the web application.
The problem is the time it takes to deploy everything, especially the databases. We are using Code First migration and MVC .NET and SQL Server, all with the latest version. It is a SaaS. And the code first migration process is able to update the database one-by-one.
The API and the web application are deployed very quickly within a few seconds. However, the databases are all updated within about 30 minutes. During that time some users got errors and cannot use the software because the API tries to target non-updated database. And worse, if during the databases update, something fails and stop, the non-updated users are stuck until we fix the issue and update the rest of the databases.
Any idea how to solve this problem and make clients happy?
PS: The web application doesn't access to the database, but only the API.
This question is somewhat opinion-based. The maintenance window approach is the easiest. If you want to do live-updating, another way would be:
Keep a version number in the database
Allow running multiple versions of the Web API side-by-side
Choose which version of the API to use by looking at the version in the database
Determine if the Web API's public interface is stable. If it is not, also find a way to allow running multiple web sites side-by-side and choose which one based on the version in the database
The most maintainable way to accomplish this would probably be to have at least 3 servers:
One backend server which hosts the old version
One backend server which hosts the new version
The frontend server which routes users to the proper backend server based on the current version.
The routing could take place only at login, or you could do something more fancy such as redirecting the logged-in user when an upgrade is detected. Obviously none of this deals with what happens to one particular client during the actual upgrade of that client's database. You'll still need to address that separately.

How to put database on azure website

I recently finished my asp website, bought the domain and azure hosting. Unfortunately, when I try to do anything that accesses the database I just get stuck on a loading screen. I just used the default asp razor mvc 5 project I think for the database along with a couple of custom tables.
So here is how I would do it since you have an existing database. I would follow azures website, and set up a SQL database. I would then script out the database on you local machine, and log into your azure database using SQL Server Express. Execute the script you generated from localDB. Then all of your data will be good to go. You will then need to take the connection string from azure, and put it in your applications config file. After modifying the connection string in your application you should redeploy, and verify everything is working the way you want. You will be able to verify your data is there in SQL Server Express.
Here is a link that may be of quite a bit of assistance to you. It includes step by step instructions as well as screen shots.
Another option is using this tool. I have never used it, but I know a few people who have had great success with it.

Reading from a confidential file

(See what I did there?)
I am developing a WinForms application which needs to retrieve information from a file which contains sensitive information. The information retrieved is used to perform some complex calculations, but it includes things like salaries of certain pay bands for employees of a large company. The WinForms application will eventually need to be deployed to members of that company, but I need to make sure that I do not reveal the contents of this file to them.
The file itself is a JSON file, and is currently stored locally within the Visual Studio project file structure.
If I was to "Publish" this application through Visual Studio's Build menu, and release it through a web link, would people be able to open up this JSON file and view it? If so, is there some way this can be avoided? I have considered storing the file online and accessing it via HTTP request, however I don't really know much about that so could do with some advice.
Cheers,
Josh
If I was to "Publish" this application through Visual Studio's Build menu, and release it through a web link, would people be able to open up this JSON file and view it?
Yes.
If so, is there some way this can be avoided?
Only by not publishing the file.
You should look into storing this information in a database that can only be accessed through an authorised account via HTTPS. I'd recommend using WCF as it will integrate well with C# and WinForms. The best approach would be to perform the calculations on the server side (either in the WCF service itself or as stored procedures in the database). Thus you only need to gather the inputs on the client, pass these back to the server and then display the result.
You can also do things like logging all attempts (successful or not) to access this data so you have a complete audit trail. You can also expose your WCF service to other clients if necessary.
I would look into creating a separate (WebAPI or WCF) service that has access to that file and knows how to serve up the public facing portions of it to your application.
So let's assume the file lives at \\hrserver\C$\sensitive.dat. Your service has access to that file, but the client applications do not. Your client applications access the service (https://hrserverhelper/GetHrData), which encapsulates the authentication/authorization to that file. It then parses out the sensitive data (perhaps from the JSON you already are set up to create for that file), and passes the non-sensitive data to your client application.
If it turns out that all the data in the file is sensitive, then have your service provide operations to perform the calculations that your WinForms app performs currently. For example, your WinForms app submits the inputs it wishes to perform to a WebMethod that knows how to perform those calculations with the sensitive data - the WebMethod spits out the results.
However, in this scenario, be aware that basic mathematical skills will likely be able to reverse engineer the "sensitive" data here. If I submit 2 and get back 4, and I submit 3 and get back 6, I'll assume the "sensitive" number is 2.

Set ItemSource/DataSource of an AutoCompleteBox in Windows Phone 8 to a database table created in PHPMyAdmin

Two parts to this question
So I've successfully got an AutoCompleteBox working with a local class file called Search.cs I followed this tutorial in order to do so using the IENumerable interface to bind the AutoCompleteBox to the "Search" class file.
Now I'm wondering, is it possible to do the same, except retrieve results from a table found in a Database in PHPMyAdmin on a hosted server? If so, how would I go about doing this?
How do you create get the Search Results to "Do Something", say if I wanted the search result to link to an external webpage via a basic hyperlink, or to another page in-app, what would I have to modify or add in? Any help is much appreciated.
phpMyAdmin is a tool to manage a MySQL database rather than a database itself, so you probably mean you want to connect to a hosted MySQL instance. This is possible, but what you probably want to do is write some server-side software that exposes an API rather that having full database access publicly available.
So then you'll have the server-side application waiting to be called. The Windows Phone application will fire off a request and wait for the response. The server application, when it gets that request, will do all the proper sanitizing of user-input data before running your query on the database and returning the properly-escaped results. How exactly you implement that API is up to your needs. Once the Windows Phone application gets back the response, it can build the page it displays to the user, which, yes, you can include hyperlinks. Make that part of the API response if you store the URL in the database.

Direct user to different page based on network connection

I am currently working on upgrading an existing asp.net web application. It is being hosted on our local web server and I would like to direct users to a different page if they are connecting locally.
Example: An employee connecting on our local network would be directed to page A. If someone on an outside network connects (presumably a customer) they would be directed to page B. Is this possible? Does the web server know if the connection is local or not? If so, I should be able to code for it. Any help or suggestions would be appreciated.
We've had an embarrassingly awful hack in place for this kind of situation for years. And while I'm sure there's a better way, what do do is look at the System.Web.HttpContext.Current.Request.UserHostAddress and if it matches a list we go to one page, otherwise we go another.

Categories

Resources