How can I access the DataContext via Project Dependency? - asp.net-mvc

I have setup a MVC 5 Solution that has two sub projects. 1) The MVC Application and 2) an API.
I am using EF 6 in the MVC Application, and I have set up a project dependency from the API to the MVC application. However, when I try to use scaffolding in the API project, I get this error: "Unable to retrieve metadata for Chat.Message. No connection string named "ChatEntities" could be found in the application config file.
Any advice on how to resolve this error?

Just copy connection string to the web.config file in API project.

Related

ASP.NET Core Identity for multiple project with IdentityServer4

I have one solution with two MVC projects, which use the IdentityServer4. In one project I installed the IdentityServer4 and have full access to the database. The other project is an MVC client.
When I set the [Authorize] attribute on both project all works fine, but this role attribute [Authorize(Roles = "user")] works only in project one that has the IdentityServer4, the MVC client says:
Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Entities.Application.ApplicationUser]' while attempting to activate 'IdMWeb.Controllers.AccountController' (this is the project one with the IdentityServer4 installed).
My questions are, why project one does not complain when I do not set the role attribute in project 2.
Also, how can project 2 get the role from the database?
please see bottom link for asp .net core apps:
Share authentication cookies path
simple answer:
please add bottom code in startup.cs ConfigureServices method of two or more ASP.NET Core projects :(be cureful the path "C:\sampleDirectoryInServer" exists in your server)
services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(#"C:\sampleDirectoryInServer"))
.SetApplicationName("SharedCookieApp");
services.ConfigureApplicationCookie(options => {
options.Cookie.Name = ".AspNet.SharedCookie";
});
it's work for me(2 asp.net core app with same database and different ports)
Based on the error message your posted, It may be that you did not define a client on the server side for this particular client. A IdentityServer instance will only accept requests from clients that it already knows about. You have to define the acceptable clients for it when you configure the middleware.

Create access token using identity framework with exist mvc 5 project?

I want to generate access token by using identity framework to communicate with my android app. Type of my project just default MVC project not API project .
Anyone can give me some examples?
I found the solution on this website
http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/

How can remove connectionstring from webconfig?

I have a project that has 3 layers.
1)Interface (It is an MVC project with View and Controller)
2)BI (It is a class library project)
3)DAL (It Is A class library that work with Entity Framework DataBase First)
My Problem :
There is a connection string in webconfig of the first project(MVC). But I want remove it. then the other layers should use of DAL Connection Strings.
But when I delete the connection string from that it cant connect and work with DB !!
I remember when I was working with ASP.NET and Linq to sql it was posible.
Please help me.
Once deployed, the only configuration that will be available will be the web.config of your MVC project. Instead of removing the connection string you could just encrypt it.
ASP.NET Connection String Encryption / Protection
Programmatically encrypting a config-file in .NET
http://msdn.microsoft.com/en-us/library/89211k9b.aspx
Personally I'm using a mechanism based on that blog post. I have an administrative api call on my web application to send the information that must be encrypted.
- http://weblogs.asp.net/sukumarraju/archive/2009/09/28/encrypt-and-decrypt-connectionstring-section-in-web-config.aspx

Report Wizard in an MVC Application

After some research, I understand that we can use sql server reporting in an mvc application as long as the view engine is web form instead of razor.
The tutorial I have been trying to follow is:
Creating an ASP.net reporting using Visual Studio 2010
The problem is in Part 2. I cannot find the local classes listed as Data Source. The only choice is to set up a new xsd file to connect to the database. How can I have my reports to use my data repositories as their data source?
The credit goes to TNCodeMonkey!
Not only the project has to be compiled first, we must have an index.aspx file in the root folder of the MVC Web Application.
I don't know why. But the Data source is populated with all my dll's once that magic file is in place.

Should the connectionstring come from the mvc project or the data project?

When using a separate project to handle db operations in an mvc project, should I store the connectionstring in the dbproject (using app.config) or should I keep the connectionstring in the web.config of the mvc project, and inject it into the repository (provided by the dbproject) when instantiating it?
The config is always read from the outermost app. The web.config is where your connectionstring should be.
You can read it from your db project just as if it were the main project. You don't need to inject or do anything special.

Resources