B2C Authentication with WebForms - 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

Related

OWIN Startup class not auto-detected despite having Startup.cs in matching assembly name and global assembly name

I created a web API and added the OWIN middleware to the project, i.e. Startup.cs, the Startup.Auth.cs, and OpenIdConnectCachingSecurityTokenProvider.cs (as needed for Azure AD B2C).
However, my Web API requires that I use the assembly attribute [assembly: OwinStartup(typeof(My_Web_API_2.Startup))] in Startup.cs, otherwise the OWIN startup class isn't detected. The TaskService app in the Azure AD B2C sample doesn't require the attribute to work.
The OWIN Startup class detection docs state that
Naming Convention: Katana looks for a class named Startup in
namespace matching the assembly name or the global namespace.
But my web API is using a matching assembly name and global namespace? What am I missing from my web API to detect the OWIN Startup class?
The assembly name has -'s but the namespace has _'s.
This probably happened when you performed a File -> New Project. VS converted the hyphens to underscores in the namespace for you (and didn't tell you!).

ASP.Net MVC 5 -Attempt by security transparent method 'WebMatrix.WebData.SimpleMembershipProvider.ValidateUser to access security method failed

I am using Simplemembership in my asp.net web api project. When i try to validate the user using the below code, i get the error(Error 1)
if (!Membership.ValidateUser(username, password))
{
throw new ValidationException("Username or pwassword is incorrect.");
}
Error 1:
Attempt by security transparent method
'WebMatrix.WebData.SimpleMembershipProvider.
ValidateUser(System.String, System.String)' to access security
critical method
'System.Web.WebPages.StringExtensions.IsEmpty(System.String)' failed.
Earlier i got a similar error(Error 2) which i resolved by installing these packages:
Install-Package Microsoft.AspNet.WebHelpers
Install-Package Microsoft.AspNet.WebPages.Data
Error 2:
Attempt by security transparent method
'WebMatrix.WebData.PreApplicationStartCode.OnConnectionOpened(
System.Object, WebMatrix.Data.ConnectionEventArgs)' to access security
critical method 'System.Web.WebPages.
HttpContextExtensions.RegisterForDispose(System.Web.HttpContextBase,
System.IDisposable)' failed.
I did clean the solution and tried, still getting the same error,
Do i need to install any more packages or does even the simplemembership work in web api?
The current fix for me was by upgrading to .Net 4.5.3. I figured this out of frustration. This issue doesnt just affect MVC 5 but core Webmatrix projects as well after upgrading to webpages 3.2.3. I think it is a framework issue that will be fixed with the new Microsoft Identity. The current fix for me is below:
Note: Please use the property pages wizard in visual studio to change your target framework to .Net Framework 4.5.3. It will update your web.config
<compilation debug="true" targetFramework="4.5.3">
<assemblies>
<add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</assemblies>
</compilation>
Step 1: Install-Package Microsoft.AspNet.WebHelpers
Step 2: Install-Package Microsoft.AspNet.WebPages.Data
Step 3: [Optional] Install-Package Owin
Step 4: Change targetFramework to .Net 4.5.3 via Property pages dialog box

Websecurity not working

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.

Configure Active Directory in MVC3 project

I have a problem deploying a MVC3 project IIS server, the project runs well in my local machine, but not in the server the errors say's this
Parser Error Message: The supplied credential is invalid.
This is the line of configuration of my merbership Provider
<add name="MembershipADProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" applicationName="AtentoCNEHoraExtra" connectionStringName="ADConnectionString" attributeMapUsername="sAMAccountName" />
What could be wrong???
I found the solution to my problem, I have to modify the identity of the process model associated with the pool .NET 4 in the configuration on my IIS
The value that has by default was ApplicationPoolIdentity and I have to changed to NetworkService
This configuration can be made in ISS --> Application Pool ----> .Net 4---> Advanced Settings --> Process Model
I hope that helps to others

VS2010 development web server does not use integrated-mode HTTP handlers/modules

I am developing an ASP.NET MVC 2 web site, targeted for .NET Framework 4.0, using Visual Studio 2010.
My web.config contains the following code:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="XhtmlModule" type="DomenicDenicola.Website.XhtmlModule" />
</modules>
<handlers>
<add name="DotLess" type="dotless.Core.LessCssHttpHandler,dotless.Core" path="*.less" verb="*" />
</handlers>
</system.webServer>
When I use Build > Publish to put the web site on my local IIS7 instance, it works great.
However, when I use Debug > Start Debugging, neither the HTTP handler nor module are executed on any requests.
Strangely enough, when I put the handler and module <add /> tags back into <system.web /> under <httpHandlers /> and <httpModules />, they work. This seems to imply that the development web server is running in classic mode.
How do I fix this?
You don't. WebDev.WebServer.exe does not and cannot support integrated pipeline.
So, if you have code that cannot be written to perform in both environments you will need to use a local IIS for development.
Basically, system.web is the place to configure webdev server and IIS5-6 handlers and modules. system.webServer is for IIS7 handlers and modules, as you know.
Reference:
Each request in WebDev.WebHost40 (and previous versions) is processed by HttpRuntime.ProcessRequest
(which does not support integrated pipeline mode). This is the method used in all three versions of WebHost.WebServer.dll (the core of WebDev.WebServer.exe)
And the word of some dude who is fairly familiar with the inner workings of Cassini/WebDev by virtue of managing this project. ;-)
I'm not sure if I'm too late in answering, but while it's known fact that the Cassini server doesn't support integrated pipeline mode, you can still test locally using the classic pipeline by adding it to the httpModules section of system.web in your web.config:
<system.web>
<compilation debug="true" targetFramework="4.0"/>
.
.
.
<!-- HTTP Modules using Classic Pipeline -->
<httpModules>
<add name="YourHttpModule" type="ACME.YourHttpModule"/>
</httpModules>
</system.web>
<system.webServer>
<!-- HTTP Modules using Integrated Pipeline -->
<modules runAllManagedModulesForAllRequests="true">
<add name="YourHttpModule" type="ACME.YourHttpModule"/>
</modules>
</system.webServer>
You'd probably want to remove the httpModules section from your production web.config.
Got this today while running in visual studio 2012. Found the reason was visual studio launched the old web server that comes with 2010 and as explained above it can't work there. Changed to IIS Express by right click on properties, choose "Web" tab and and selecting IIS Express option. Then launching debug mode will start in IIS Express and this aparently supports operations such as Request.Headers.Add or whatever caused your exception.

Resources