Get One MembershipUser from MembershipUserCollection

北城以北 提交于 2019-12-24 11:47:03

问题


I have a membershipusercollection that I am trying to get one specific user from. Unfortunately I cannot user the MembershipUser.GetUser() method. So currently I have a collection of all membership users like so:

MembershipUserCollection mc = Membership.GetAllUsers();

What I would like to do is get one of these users by either login or email (doesn't matter which). I realize I can do a foreach loop on the collection and compare but I have to imagine there is a better way....

Thanks in advance...

For those curious why I cannot user the getuser method, lets just say it is because of sharepoint.


回答1:


Try this

mc.Cast<MembershipUser>().SingleOrDefault(m => m.UserName == "username");



回答2:


MembershipUser.GetUser() is not a valid method instead Membership.GetUser("johndoe").

var user = Membership.GetAllUsers()
    .Cast<MembershipUser>()
    .FirstOrDefault(m => m.Email == "john@doe.com" || m.UserName == "johndoe");

One thing you want to watch out is GetAllUsers retrieves all users from database.




回答3:


Apparently there is another way:

MembershipUser m = mc["username"];


来源:https://stackoverflow.com/questions/15411951/get-one-membershipuser-from-membershipusercollection

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