How To import ASP.NET Identity into ASP.NET MVC Boilerplate - asp.net-mvc

I would like to share how to successfully import ASP.NET Identity into ASP.NET MVC Boilerplate using Visual Studio 2013.

I found lots of questions, but no definitive answers.
So here's what I came up with:
Decide on a project\solution name
The two projects\solutions must use the same names
Create them in seperate directories
Projects\Web\<SolutionDir>
Projects\MVC\<SolutionDir>
Create project from template - ASP.NET Web Application
Select a template - MVC
Rebuild Solution
Close Solution
Create project from template - ASP.NET MVC Boilerplate
Right-click Project
Properties
Make sure Assembly name and Default namespace is the same
Rebuild Solution
There may be some missing namespaces
Double-click the build errors to open effected code
ReSharper can help automatically regenerate the missing namespaces
Right-click References
Manage Nuget Packages
Install packages:
Microsoft.Owin.Host.SystemWeb
Microsoft.AspNet.Identity.Core
Microsoft.AspNet.Identity.OWIN
Microsoft.AspNet.Identity.EntityFramework
Microsoft.Owin.Security.Google
Copy the following files and folder structure from ASP.NET Web project into ASP.NET MVC project:
files:
App_Start\IdentityConfig
App_Start\Startup.Auth
Controllers\AccountController.cs
Controllers\ManageController.cs
Models\AccountController
Models\IdentityModels
Models\AccountViewModels
Startup.cs
folders:
Views\Account
Views\Manage
Insert the following into Configuration method of Start.cs:
ConfigureAuth(app);
Find following section in Web.config from ASP.NET Web project and copy into Web.config in ASP.NET MVC project:
<connectionStrings>
<add
name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\..."
providerName="System.Data.SqlClient" />
</connectionStrings>
Rebuild Solution
References:
Adding ASP.NET Identity and Authorisation control to your MVC website
Trying to add Authentication to ASP.NET MVC Boilerplate template

Related

Can you add ASP.Net MVC 4 Intranet Template to existing empty project

I started a Visual Studio 2012 project by creating an asp.net MVC 4 empty project. I would like to know if I can add the authentication membership provider, bundling, css, javascript that come packaged with the asp.net mvc 4 intranet site project template to my existing empty project without having to create from scratch.
Create new one. Copy classes from App_start folder, global.asax and web.config to your project

Using the latest source of ASP.NET MVC4 RC1 in a project

My machine has ASP.NET MVC3 and ASP.NET MVC4 beta1 installed.
I just donwloaded the latest source of ASP.NET MVC4 from http://aspnetwebstack.codeplex.com/SourceControl/list/changesets
How can I create a project in VS2010 that uses the assemblies created from this sourcecode?
You may take a look at the following blog post. It's about an older version of ASP.NET MVC but the same applies for the MVC 4.0 Beta.
So here are the steps:
Create a new ASP.NET MVC 4 project in VS
Remove the reference of the System.Web.Mvc assembly (or whatever assembly you will be trying to debug)
Add a reference to the assembly you have compiled
In your web.config make sure that you remove the corresponding <add assembly="System.Web.Mvc, ..."> (the one with the strong name) or it will use the assembly from the GAC instead of your custom built one.
I downloaded the source at http://aspnetwebstack.codeplex.com/ and used the instructions there and there http://aspnetwebstack.codeplex.com/documentation to get a compiling solution with working unittests.
I then created a new MVC4-Beta1 project in the solution. I removed references to
System.Web.Mvc.dll
System.Web.WebPages.dll
and added references to the corresponding projects in the solution.
I then changed web.configs in the main folder and view folder.
<add key="webpages:Version" value="2.0.0.0" />
was changed to
<add key="webpages:Version" value="3.0.0.0" />
I can now compile the project and debug into the MVC-source.

Converting a ASP.NET MVC 3 Project to MVC 4

What steps should I take to safely upgrade an existing ASP.NET MVC 3 project to the latest ASP.NET MVC 4 Developer Preview?
The major change is to upgrade the relevant references to the latest version (4.0 /2.0). You need to update your web config files to upgrade the version of the following namespaces
System.Web.Mvc ( Change to 4.0.0.0)
System.Web.Webpages (Change to 2.0.0.0)
System.Web.Helpers (Change to 2.0.0.0)
System.Web.WebPages.Razor (Change to 2.0.0.0)
Also you need to update the root level web config file to have these appsettings entries
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="PreserveLoginUrl" value="true" />
</appSettings>
Then you need to update the DLLs referenced in the project. Remove the existing MVC3 dll reference and add MVC4 (use nuget package manager to do this)
This link handles all aspects of the conversion. Refer it as needed.
Follow the official steps posted here:
Upgrading an ASP.NET MVC 3 Project to ASP.NET MVC 4
Note: These steps cover the official release--not the developer preview.
Better yet, use the auto-update tool:
https://www.nuget.org/packages/UpgradeMvc3ToMvc4
Your best bet it's installing the MVC4 from the platform installer.
Then follow the steps on the official asp.net/mvc/mvc4 page. If you follow (only) the Shyju instructions It wouldn't work.
When you end with the official instructions, look on your Views directory, and you may find another web.config with another reference to the MVC assembly and several others for razor.
You also want to change the versions of this lines. Basically replace every 3.0.0.0 you find for 4.0.0.0 and 1.0.0.0 for 2.0.0.0
IF YOU DON'T MAKE THIS LAST CHANGE, YOU MAY END WITH THE ERROR ON THE FOLLOWING QUESTION
Look for included dll's and make sure they're for the correct versions.

Issue with Razor MVC3

MVC 3 RC
I registered both System.Web.Razor and System.Web.WebPages.Razor and still have System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup not recognized in my View/web.Config
Intellisence for Razor is not working
What can be wrong with my configuration
Add the assembly "System.Web.WebPages.Razor" to the main Web.config of the application.
For some reason the "ASP.NET MVC 3 Web Application" project template included in the RC1 does not fill it.
<add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
It's not obvious from your question or the comments if you've done it, but in order to have full support for Razor in your system you need to install the runtime and the tooling using the following installer: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=a920ccee-1397-4feb-824a-2dfefee47d54
Just putting the MVC 3 runtime binaries in your application and making changes to web.config is not enough to make Razor IntelliSense work in Visual Studio.
The problem disappeared from the time I removed Resharper

Using asp.net mvc 2 features with the spark view engine

I am working with an ASP.NET MVC project which was originally started from the CodeBetter.Canvas project - and I'm trying to move to ASP.NET MVC 2. I successfully upgraded my project using Eilon's upgrade tool, moved to VS2010 (although not yet to .NET 4).
The issue I'm having currently is only occurring when using the spark view engine. Here is the relevant bit of code in my View.spark (strongly typed):
${Html.EditorFor(e => e)}
The same bit of code works just fine if I use an .aspx view:
<%= Html.EditorFor(e => e) %>
The major point here being "EditorFor" is new in ASP.NET MVC 2 and in my project I can use that helper in an ASPX view but not a Spark view.
I've tried upgrading Spark to use MVC 2 (as well as MvcContrib and Ninject), thinking maybe it was one of those that was freaking out - but so far no luck - I'm still seeing the same behavior.
Here is the full error message that is thrown from within Spark's BatchCompiler class.
Dynamic view compilation failed.
(0,0): warning CS1701: Assuming
assembly reference 'System.Web.Mvc,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35'
matches 'System.Web.Mvc,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35', you
may need to supply runtime policy
c:\inetpub\wwwroot[myproject]\CodeBetter.Canvas.Web\Views[MyEntity]\View.spark(9,16):
error CS1061:
'System.Web.Mvc.HtmlHelper' does not
contain a definition for 'EditorFor'
and no extension method 'EditorFor'
accepting a first argument of type
'System.Web.Mvc.HtmlHelper' could be
found (are you missing a using
directive or an assembly reference?)
Here is the Spark related code in my Global.asax:
var settings = new SparkSettings()
.AddNamespace("System")
.AddNamespace("System.Collections.Generic")
.AddNamespace("System.Web.Mvc")
.AddNamespace("System.Web.Mvc.Html")
.AddNamespace("MvcContrib.FluentHtml")
.AddNamespace("CodeBetter.Canvas")
.AddNamespace("CodeBetter.Canvas.Web")
.SetPageBaseType("ApplicationViewPage")
.SetAutomaticEncoding(true);
#if DEBUG
settings.SetDebug(true);
#endif
var viewFactory = new SparkViewFactory(settings);
ViewEngines.Engines.Add(viewFactory);
Also, I am referencing System.Web.Mvc.Html in my spark view as mentioned in another SO answer.
<use namespace="System.Web.Mvc.Html" />
The underlying issue is that a version 1.0.0 assembly is still being referenced somewhere in the project.
Using reflector to examine each assemblies dependencies, I found (if you're starting with CodeBetter.Canvas project) there are three dependent projects that need to be updated to use the 2.0.0 version of system.web.mvc.dll
Spark. Download the latest version
of spark from teamcity and use the
spark.web.mvc2.dll (which references
system.web.mvc.dll 2.0.0) Link to detailed instructions.
MVCContrib. Download the latest version
of MVCContrib (download release, download source) which references the
system.web.mvc.dll 2.0.0.
Ninject. Download the latest version of
ninject and recompile the VS solution
after updating the reference to the 2.0.0
version of system.web.mvc.dll
Now, replace these dependency's assemblies in your project and update the project references if necessary. All should be well.
I recently converted my spark project to asp.net mvc 2. I dont see in what you've written that you've switched from using Spark.Web.Mvc to Spark.Web.Mvc2.
Just to be clear, Spark.Web.Mvc2 is the project that needs to be recompiled with the new System.Web.Mvc assembly.
I had issues with intellisense until i removed all assembly and namespace references from the web.config and put them in the global.spark file and had added the pageBaseType="Spark.Web.Mvc2.SparkView" attribute to the Spark->Pages node in the web.config.
Hope any of that helps.
When you downloaded newest Spark sources, did you rebuild it using MVC 2 assemblies? Did you replace references in Spark project?
Try specifiying the System.Web.Mvc reference in the web.config instead of the Global.ascx.cs file so that you can specify the specific version:
<spark>
<compilation debug="true" defaultLanguage="CSharp">
<assemblies>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</assemblies>
</compilation>
</spark>
This should prevent SPARK from working at all if the wrong version of the MVC assembly is being loaded from somewhere.
If the MVC 1.0 Assembly is in the GAC you can clear and reload it as follows:
(from Visual Studio command prompt)
ngen /delete System.Web.Mvc
ngen /delete System.Web.Abstractions
ngen update
Hope this helps,
Jeff French
These are the steps I took to resolve the issue with a new MVC 2 project and Spark 1.1:
Compile against MVC 2.0 - I double checked the references to make sure I was linking to MVC 2 and not MVC 1. Since this was a new project, this was not an issue.
Added System.Web.Mvc.Html - I added System.Web.Mvc.Html to the Spark configuration, to make sure that namespace was added to all views.
In Global.asax.cs Application_Start
var settings = new SparkSettings()
.SetDebug(true)
.SetAutomaticEncoding(true)
.AddAssembly("Web")
.AddNamespace("Web.Model")
.AddNamespace("System.Collections.Generic")
.AddNamespace("System.Linq")
.AddNamespace("System.Web.Mvc")
.AddNamespace("System.Web.Mvc.Html");
This can also be done in the webconfig in the Spark View Engine block.
Add the Typed Model - Make sure you type the Spark View Model. In aspx this is done with the Inherits in the page declaration, like this:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<MyModelType>" %>
in Spark:
<viewdata model="MyModelType" />
I tried a lot of things mentioned above but just couldn't get a dependent dll that my views used (and also referenced MVC 1.0.0.0) to use the MVC 2.0.0.0, so here is what fixed it for me...
I downloaded the Spark code and added the following line to the BatchCompiler.cs file in the Compile method:
compilerParameters.CompilerOptions = "/nowarn:1701";

Resources