SHOW:
|
|
- or go back to the newest paste.
1 | public static SPUser Find(SPWeb inWeb, string loginName) | |
2 | { | |
3 | try | |
4 | { | |
5 | if (string.IsNullOrEmpty(loginName)) | |
6 | return null; | |
7 | ||
8 | string searchedLogin = loginName; | |
9 | SPWeb web = inWeb.IsRootWeb ? inWeb : inWeb.ParentWeb; | |
10 | SPUser result = null; | |
11 | SPSecurity.RunWithElevatedPrivileges(delegate | |
12 | { | |
13 | using (var elevatedSite = new SPSite(web.Site.ID)) | |
14 | { | |
15 | using (var elevatedWeb = elevatedSite.OpenWeb(web.ID)) | |
16 | { | |
17 | result = elevatedWeb.EnsureUserUnsafe(searchedLogin); | |
18 | } | |
19 | } | |
20 | }); | |
21 | return result; | |
22 | } | |
23 | catch | |
24 | { | |
25 | return null; | |
26 | - | } |
26 | + | |
27 | ||
28 | ------------------------------ | |
29 | ||
30 | public static SPUser EnsureUserUnsafe(this SPWeb web, string logonName) | |
31 | { | |
32 | UnsafeUpdate uu = null; | |
33 | try | |
34 | { | |
35 | uu = new UnsafeUpdate(web); | |
36 | return web.EnsureUser(logonName); | |
37 | } | |
38 | catch (SPException ex) | |
39 | { | |
40 | throw new SPException("error ensure user " + logonName, ex); | |
41 | } | |
42 | finally | |
43 | { | |
44 | if (uu != null) | |
45 | { | |
46 | uu.Dispose(); | |
47 | } | |
48 | } | |
49 | } |