简体   繁体   中英

Webmatrix.WebData Login method results in exception

Hi I have been trying to integrate SimpleMembershipProvider into my asp.net mvc app but I seem to be having some problems with it.

This is what I have in my web.config:

 <membership defaultProvider="SimpleMembershipProvider">
  <providers>
    <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider"/>
  </providers>
</membership>

This is the code I am trying to run in order to Login a user:

public bool Login(string userName, string password, bool rememberMe)
{
        return WebSecurity.Login(userName, password, rememberMe);
}

Before all this get's called the following attribute code get's called and it works fine:

 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MembershipInitializerAttribute : ActionFilterAttribute
{

    private const string CONNECTIONSTRING_NAME = "eShopConnectionString";
    private const string DATABASE_TABLE_NAME = "Users";
    private const string DATABASE_TABLE_IDENTIFIER_COLUMN = "UserId";
    private const string DATABASE_TABLE_USER_NAME_COLUMN = "UserName";
    private const bool AUTO_CREATE_TABLES = true;

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (!WebSecurity.Initialized)
        {
            WebSecurity.InitializeDatabaseConnection(CONNECTIONSTRING_NAME, DATABASE_TABLE_NAME, DATABASE_TABLE_IDENTIFIER_COLUMN, DATABASE_TABLE_USER_NAME_COLUMN, AUTO_CREATE_TABLES);
        }
    }
}

The login method get's executed after I put my username and password.After I press the login button the Login Action get's executed that calls the Login method , witch throws the following exception:

{"Could not load type 'WebMatrix.WebData.SimpleMembershipProvider'. (F:\\Programare\\Projects IDE\\Visual Studio\\2012\\e-shop\\Backend\\WebApi\\e-shop.WebApi\\web.config line 73)"}

在此输入图像描述

After I did a but of research many suggested to set the WebMatrix.WebData Copy Local property to true.

After I did that it seems that something is wrong at the application startup because I no longer get to see the screen where I can insert my credentials I immediately get a screen with this message:

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: This method cannot be called during the application's pre-start initialization phase.

在此输入图像描述

What am I doing wrong and how can I correct it?

EDIT

I am also getting this StackTrace:

在此输入图像描述

Your web.config entry does not look correct. The type attribute in the add element should include the assembly name after a comma. Here is what the web.config entry should look like.

<membership defaultProvider="SimpleMembershipProvider">
  <providers>
    <clear/>
    <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
  </providers>
</membership>

Note that in addition to having the assembly name it is best to use the clear element to make sure you do not register multiple providers, which can cause strange behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM