简体   繁体   中英

Using WebMatrix.WebData.WebSecurity in a WPF application

I have an MVC4 internet project that uses the out of the box WebMatrix security. A requirement has come along to add a WPF front end to the same application. I have moved the model into a separate DLL and started building the WPF front end over the same entities.

The only issue I am having is trying to integrate with the existing security model. I have added a system.web section into my WPF project's app.config as follows:

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

Now when I call WebSecurity.Login("Username", "Password") I get the following error:

You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class. This call should be placed in an _AppStart.cshtml file in the root of your site.

I have tried calling the InitializeSimpleMembershipAttribute(); code that comes with the MVC project in the start-up of my WPF application, but it makes no difference to the above error.

I cannot find any examples on-line of how to do this, am going down a dead end here?

Any help would be appreciated.

In the end I decided to take a different approach and use a WCF authentication service to share between my different clients, as outlined in this post Walkthrough: Using ASP.NET Application Services .

This was a neater solution than trying to get web matrix to work directly from inside a WPF project.

You need to do two things to get this to work

First in Global.asax.cs add this at the bottom of Application_Start()

    var attr = new MixThread.Web.Filters.InitializeSimpleMembershipAttribute();
    attr.OnActionExecuting(null);

Second, add this to your Web.config

<system.serviceModel>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

You should be able to construct a WCF based login like so:

[AllowAnonymous]
public bool Login(string username, string password)
{
    return WebMatrix.WebData.WebSecurity.Login(username, password);
}

您可能也需要将此添加到服务中。

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

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