Using WebMatrix.WebData.WebSecurity in a WPF application

喜夏-厌秋 提交于 2019-12-13 18:33:01

问题


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.


回答1:


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.




回答2:


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);
}



回答3:


You may need to add this to your service too.

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]


来源:https://stackoverflow.com/questions/13161841/using-webmatrix-webdata-websecurity-in-a-wpf-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!