0

I've implemented a Simple Membership in my ASP.NET MVC4 application. But now I need to implemente the creation the users and verify the login in it.

So I started to copy some pieces of code of my recent project to this. By example, this is the part where crashes

            WebSecurity.InitializeDatabaseConnection("SimpleMembershipConnection",
  "UserProfile", "UserId", "UserName", autoCreateTables: true);
            bool val = WebSecurity.Login("1010", "pass"); // here throws the exception

The error says:

Object reference not set to an instance of an object. Parameter name: httpContext.

Also, I just import some libraries as WebMatrix.WebData, WebMatrix.Data, and System.Web

This is my entire app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
     ...
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
<roleManager enabled="true" defaultProvider="simple">
  <providers>
    <clear />
    <add name="simple" type="WebMatrix.WebData.SimpleRoleProvider,              WebMatrix.WebData" />
  </providers>
</roleManager>
<membership defaultProvider="simple">
  <providers>
    <clear />
    <add name="simple" type="WebMatrix.WebData.SimpleMembershipProvider,              WebMatrix.WebData" />
  </providers>
</membership>

How can I fix the exception, about not getting null in HttpContext?

Darf Zon
  • 6,268
  • 20
  • 90
  • 149

1 Answers1

1

No. There is no need in winforms to follow Membership provider lifecycle. You will not have a HttpContext set if you use a dll meant for web requests in winforms. HttpContext refers to the current web request, something that is clearly missing in winforms.

You could separate your authentication business logic and put that in a different dll. This library you could reference in both your windows application and your membership provider.

If you must authenticate your users over the web, the way to do this in a windows application is by querying a webservice. Then you could use your SimpleMembershipProvider at the server side.

nunespascal
  • 17,584
  • 2
  • 43
  • 46