Websecurity not working - visual-studio

I put this code in _AppStart.cshtml to make tables in the database (VS 2012)
The Problem is there is an error on WebSecurity i.e The name WebSecurity does not exist in th
current context
#using System.Web.Security;
#{
WebSecurity.InitializeDatabaseConnection("MembershipExample","UserProfile","UserId","UserName", true);
}

You need to add a reference to System.Web to your project (in the Project Manager on the right side, right-click References and choose Add).
You can then add a using System.Web.Security to your code file.

Related

B2C Authentication with WebForms

I have a legacy WebForms application. I need to add Azure B2C authentication to it. My application is using currently Microsoft.Aspnet.Identity. It does not have any startup.cs page. How can I make this migration?
Some minor things allow you to switch your site to use AAD or aadb2c for authentication.
Get the necessary OWIN NuGet packages, Add some startup code to use the OWIN authentication libraries ,Register your application in AADb2c.
Update your web.config with appropriate OAuth values.
Even though OWIN didn’t exist when you wrote your Web Forms app, you can still use it and it will significantly simplify the process.
In the Visual Studio menu, select Tools > NuGet Package Manager > Package Manager Console.
Make sure your web project is selected in the Default project drop down.
Run the below commands:
install-package Microsoft.Owin.Host.SystemWeb
install-package Microsoft.Owin.Security.OpenIdConnect
install-package Microsoft.Owin.Security.Cookies
You may check the references and then follow this B2C-WebForms-DotNet
References:
c# - ASP.NET Web Api - Startup.cs doesn't exist - Stack
Overflow
c# - OWIN Startup Class Missing - Stack Overflow
edit:
My web forms if for visual C# and could find owin start up :
Right click on App_Start > add item > under web , you can find it.
Please check if your project is visual c# .
edit 2:
Please check if below points help :
Maybe you can create new start up class by using owin library in the clas
Imports Microsoft.Owin.Extensions
Imports Microsoft.Owin.Security
Imports Microsoft.Owin.Security.Cookies
Imports Microsoft.Owin.Security.OpenIdConnect
Imports Owin
Imports System
Imports System.Configuration
Check the web.config file and remove this key setting and try
<add key="owin:AutomaticAppStartup" value="false" />
and also try add <add key="owin:AutomaticAppStartup" value="true " /> in web.config file
The following attribute will set the startup class to the StartUp class in the StartupDemo namespace.
[assembly: OwinStartup(typeof(StartupDemo.StartUp))]
Add appSetting element in the Configuration file
<appSettings>
<add key="owin:appStartup" value="StartupDemo.StartUp" />
</appSettings>
Check how owin detects start up owin-startup-class-detection -Microsoft docs
Other references:
owin-startup-class-not-detected
owin-with-vb-net-legacy-code-asp-net-web-forms
where-to-implement-startup-code-in-vb-net

Type controller does not have a default constructor

Hi friends in webapi using mvc format i created one webapi project here i added autofac resolver to resolve DI(dependency injection) its working fine then in next step i took this dlls and created new project for this i added the dlls now in this project i added one controller but it is showing the error 'x(controller name)' Type controller does not have a default constructor but for this many people given solution for this add new bootstrap class(which contain autofac for di resolve) but i am inherting my old global.asax file from my current global.asax file so i think dont want to add again all the stuff the autofac resolver will get from old dlls so i find many solutions but now luck can any one help me
Note : please ask me incase of any clarity (my english is not good excluse me for this)
The Mistake I did is Spelling mistake this is if Controller name is SampleController i am putting SampleContoller (missing r in Controller splleing) thats it now i corrected splleing now its working fine

Make .dbml file in a class library use connectionstring from config file without subclassing it

I have a class library that is referenced from a web application. The class library defaults to using TestProject.Properties.Settings.Default.NFU_Custom_Website_DataConnectionString1. I would like it to get the connectionstring from ConfigurationManager.ConnectionStrings.
I can't change the .designer.cs file because it will get overwritten. If possible I would like to avoid creating a class which inherits from the .dbml file and setting the connection string in there. My boss recommends creating a web application project and deleting all of the default.aspx etc files from it and using that instead of a class library, is this a viable solution?
Thanks
Joe
You can do that. We had done that inadvertently and I got this problem when I switched to a class library. Rather than switch back I found the answer here: Point connectionstring in dbml to app.config
In summary:
Set the connection property of the DBML to (none) in the designer. This will remove the default constructor from the DB Context class.
Create a partial class for your DB Context with a default constructor:
using System.Configuration;
namespace TestProject
{
public partial class MyDBContext
{
public MyDBContext() : base(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString)
{
OnCreated();
}
}
}

How do you add htmlHelpers into the Spark virew

I want to use the htmlHelpers in my spark view but I keep getting the following errors.
error CS0234: The type or namespace name 'Mvc' does not exist in the
namespace 'System.Web' (are you missing an assembly reference?)
I have added the System.Web.Mvc assembly into the project. I have also added the following code into the module (just for the sake of getting it working - I'll probably need to add this code to the bootstrapper --- not sure how to do that yet!)
var settings = new SparkSettings()
.SetDebug(true)
.SetAutomaticEncoding(true)
.AddAssembly("System.Web")
.AddAssembly("System.Web.Mvc")
.AddNamespace("System.Web.Mvc")
.AddNamespace("System.Web.Mvc.Html");
I also tried adding the namespace to a _global.spark file
Can someone tell me exactly what I must do to use the htmlHelpers in my spark view please.
The default Spark base view for Nancy doesn't include the public HtmlHelper Html { get; set; } property.
You can see the default view here.
The Spark view implemented for MVC integration is here, and you'll see the Html property exposed, which allows your Spark view to access it and invoke helpers.
In theory, you can inherit from NancySparkView, and specify that as your base view in your Spark settings, and add that property along with references to System.Web.Mvc etc in that class and your views should then be able to call into the helpers assuming everything is referenced correctly.
I'm not a Nancy expert, but I'm sure the type of the View is different than that of Asp.Net MVC. So, theoretically, you shouldn't be able to use MVC helpers, since they require the Html property on the View.

MEF and MVC 3 - how to load embedded views dynamically from mef container?

I'm building an MVC 3 application where MEF is used. The main idea is to have plug-in mechanism where models, controllers and views are loaded dynamically during runtime from mef container.
Each plugin/module consists of two assemblies:
Module1.Data.dll (contains definitions of models)
Module1.Web.dll (contains controllers and views)
and are put in the Plugins directory inside web application bin:
WebApp/Bin/Plugins/Module1.Data.dll
WebApp/Bin/Plugins/Module1.Web.dll
WebApp/Bin/Plugins/Module2.Data.dll
WebApp/Bin/Plugins/Module2.Web.dll
WebApp/Bin/Plugins/ModuleCore.Data.dll
WebApp/Bin/Plugins/ModuleCore.Web.dll
etc...
There is also core module that is referenced by all other modules: ModuleCore.Data.dll and respectively ModuleCore.Web.dll.
Then, in Global.asax, container is build in the following way:
AggregateCatalog catalog = new AggregateCatalog();
var binCatalog = new DirectoryCatalog(HttpRuntime.BinDirectory, "Module*.dll");
var pluginsCatalot = new DirectoryCatalog(Path.Combine(HttpRuntime.BinDirectory, "Plugins"), "Module*.dll");
catalog.Catalogs.Add(binCatalog);
catalog.Catalogs.Add(pluginsCatalot);
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
AppDomain.CurrentDomain.AppendPrivatePath(Path.Combine(HttpRuntime.BinDirectory, "Plugins"));
CustomViewEngine is created and registered and used for finding views in module assembly:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CustomViewEngine());
controller factory for loading controllers from container:
ControllerBuilder.Current.SetControllerFactory(new MefControllerFactory(_container));
and also custom virtual path provider for getting assemblies from container:
HostingEnvironment.RegisterVirtualPathProvider(new ModuleVirtualPathProvider());
Ok, so the whole infrastructure for handling pluggable models, controllers and views are ready. Now everything works... except one thing - strongly typed views.
To ilustrate the problem in more details, let's prepare the scene:
UserDTO model is located in Module1.Data.dll
ShowUserController.cs is located in Module1.Web.dll/Controllers/
Index.cshtml is located in Module1.Web.dll/Views/ShowUser (with declared #model Module1.Data.UserDto)
Now we do the following:
Run the application and go to HOST/ShowUser/Index (action method Index is executed on ShowUserController and view Index.cshtml is fetched)
After the view Index.cshtml is fetched - compilation starts (by RazorBuildProvider)
Exceptions is thrown: "cannot find Data type in namespace Module1", in other words UserDTO couldn't be found during building the view dynamically
So it seems that compiler/builder didnt look through bin/Plugins folder for Module1.Data.dll, because when I copied this file into bin folder - it worded fine.
Question/problem: why builder didn't look into bin/Plugins folder even though this directory was added by AppDomain.CurrentDomain.AppendPrivatePath method?
How to add private paths for assembly builder once so that plugins folder will be taken into consideration??
I have managed to do some work around by creating CustomRazorBuildProvider that overrides standard one:
public class CustomRazorBuildProvider : RazorBuildProvider
{
public override void GenerateCode(System.Web.Compilation.AssemblyBuilder assemblyBuilder)
{
Assembly a = Assembly.LoadFrom(Path.Combine(HttpRuntime.BinDirectory, "Plugins", "Module1.Data.dll"));
assemblyBuilder.AddAssemblyReference(a);
base.GenerateCode(assemblyBuilder);
}
}
but the drawback of this solution is that everytime the view is compiled, references to all assemblies in Plugins folder need to be added, which can cause performance issues later on when lots of plugins will be used.
Any nicer solutions?
Here is a thought.
If you follow the View Model Pattern then instead of sending the DTO's straight to the view use a ViewModel that is would be located in the same assembly as the View.
So Instead of:
UserDTO model is located in Module1.Data.dll
ShowUserController.cs is located in Module1.Web.dll/Controllers/
Index.cshtml is located in Module1.Web.dll/Views/ShowUser (with declared #model Module1.Data.UserDto)
You would have:
UserDTO model is located in Module1.Data.dll
ShowUserController.cs is located in Module1.Web.dll/Controllers/
UserVM located in Module1.Web.dll/ViewModels
Index.cshtml is located in Module1.Web.dll/Views/ShowUser (with declared #model Module1.Web.ViewModels.UserVM)
Have the Controller Map your DTO's to ViewModels
See AutoMapper to help with the Mapping

Resources