Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a SharePoint 2010 site which has Zone Level security. The site has Windows Authentication in Default Zone while it has Claimed Based Authentication in Extranet Zone. A membership provider is used for Claimed Based site.

My my requirement is to add user of that membership provider to this claimed based site programmatically (c#).

Any help will be greatly appreciated.

Thanks

share|improve this question

3 Answers

up vote 0 down vote accepted

For adding claim users, you need to create a pattern as:

i:0#.f|{MembershipProvider}|{UserName}

I hope the below code gives you enough details to resolve your issue:

string userName = "dipesh";
string membershipProvider = "MyCustomMP";
string claimUser = string.Format("i:0#.f|{0}|{1}", membershipProvider, userName);
SPUser user = web.EnsureUser(claimUser);
share|improve this answer
Thanks Dipesh for the answer. – Prakash Oct 1 '12 at 5:54

The following code successfully adds claimed based user when run as console application

SPSite site = new SPSite(siteUrl);

var spWeb1 = site.RootWeb;

spWeb1.AllowUnsafeUpdates = true;

spUser = spWeb1.EnsureUser(loginName);

spWeb1.Update();

spWeb1.AllowUnsafeUpdates = false;

But we have WCF service where we implemented these code to add user and and hosted as Windows Service. When a client application invokes this methods, it fails to add user with the following exception:

SPException: The specified user i:0#.f|aspnetsqlmembershipprovider|cu09 could not be found at Microsoft.SharePoint.SPWeb.EnsureUser(String logonName)

I have also tried to run the codes in elevated mode but it also fails.

Can you shed some light why the above codes work in console application but not in WCF Service? Thanks.

share|improve this answer

If you say, it is working well in console application then, I believe it should be permission issue only. Please note that, the run with elevated won't work because you have hosted it as a windows service and there won't be app pool. Correct me if I'm wrong. The user who is running the windows service should have appropriate rights for adding user. Try to run the windows service with the current logged-in user through which you run the console application. If it is not working then, try with Farm Admin. Even if it succeed, test with other lower privileged users so that you can get exact idea of what permissions you need. I hope you resolve your issue soon.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.