NotSupportedException when casting Membership.GetAllUsers() to generic list

萝らか妹 提交于 2019-12-22 10:46:40

问题


I am trying to use LINQ on the .NET Membership.GetAllUsers() collection, for example when getting all usernames:

var usernames = Membership.GetAllUsers().Cast<MembershipUser>().Select(x=>x.UserName).ToList();

But VS tells me that it is not supported..

Membership is a part of Web.Security, same as MembershipUser

Here is my connection string (if that is of any relevance):

   <add name="ApplicationServices" connectionString="Data Source=somerserver.xx\SQLEXPRESS;Initial Catalog=bedriftsmelding;User Id=SomeUser;Password=SomePassword;Integrated Security=False" providerName="System.Data.SqlClient" />

Stack trace:

System.NotSupportedException was unhandled by user code
Message=Specified method is not supported.
Source=System.Web.Extensions StackTrace: at System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider.GetAllUsers(Int32 pageIndex, Int32 pageSize, Int32& totalRecords) at System.Web.Security.Membership.GetAllUsers(Int32 pageIndex, Int32 pageSize, Int32& totalRecords) at System.Web.Security.Membership.GetAllUsers() at Bedriftsmelding.Nettside.Models.CompanyUsers.GetUserIdFromUserName(String username) in C:\Projects\Bedriftsmelding\Bedriftsmelding.Nettside\Models\CompanyUsers.cs:line 26 at Bedriftsmelding.Tests.DTO.CompanyUsersTest.TestGetUserIdFromUserName() in C:\Projects\Bedriftsmelding\Bedriftsmelding.Tests\DTO\CompanyUsersTest.cs:line 16 InnerException:


回答1:


Looks like you're using a ClientFormsAuthenticationMembershipProvider rather than the default SqlMembershipProvider.

According to the docs for ClientFormsAuthenticationMembershipProvider, GetllAllUsers is not supported and throws a not supported exception.

http://msdn.microsoft.com/en-us/library/system.web.clientservices.providers.clientformsauthenticationmembershipprovider.getallusers.aspx

It appears that only the basic ValiateUser and a few other functions work with this provider, as it's designed for use with the Ajax extensions. None of the user functions work, such as GetUser, GetAllUsers, DeleteUser, CreateUser, etc...




回答2:


I was facing the same problem until I read through this topic: SimpleMembership, Membership Providers, Universal Providers and the new ASP.NET 4.5 Web Forms and ASP.NET MVC 4 templates .

A simple answer is that the membership machanism in MVC4 is totally different from those in MVC3 and before. And there is other information from PM of asp.net MVC project. I think it is a trend and it is worth trying to rebuild my relevant codes again from the scratch.



来源:https://stackoverflow.com/questions/12905343/notsupportedexception-when-casting-membership-getallusers-to-generic-list

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