SCIM (System for Cross-domain Identity Management) library for C#

时光怂恿深爱的人放手 提交于 2019-12-04 03:24:53

I've updated my original answer to hopefully provide some better information.

A) This library should (hopefully) be what you're looking for:

Microsoft.SystemForCrossDomainIdentityManagement

https://www.nuget.org/packages/Microsoft.SystemForCrossDomainIdentityManagement/

One of the authors of the project recently updated it to include v1 and v2 SCIM object support and you were absolutely correct with your links to the blog posts which explains the library's purpose.

http://blogs.technet.com/b/ad/archive/2015/11/23/azure-ad-helping-you-adding-scim-support-to-your-applications.aspx

(The author has now added this to the summary on nuget so people who find the nuget library before reading the blog post won't be as confused as I was).

Here's an example of deserialzing a user based on a GET request (to Facebook), you can easily create a new user object and set it's properties etc. before POST or PUT'ing it into the system.

public static async Task GetUser()
{
    var oauthToken = "123456789foo";
    var baseUrl = "https://www.facebook.com/company/1234567890/scim/";
    var userName = "foo@bar.com";

    using (var client = new HttpClient())
    {
        // Set up client and configure for things like oauthToken which need to go on each request
        client.BaseAddress = new Uri(baseUrl);

        // Spoof User-Agent to keep Facebook happy
        client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", oauthToken);

        try
        {
            var response = await client.GetAsync($"Users?filter=userName%20eq%20%22{userName}%22");
            response.EnsureSuccessStatusCode();
            var json = await response.Content.ReadAsStringAsync();

            // This is the part which is using the nuget library I referenced
            var jsonDictionary = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(json);
            var queryResponse = new QueryResponseJsonDeserializingFactory<Core1EnterpriseUser>().Create(jsonDictionary);
            var user = queryResponse.Resources.First();                    
        }
        catch (Exception ex)
        {
            // TODO: handle exception
        }
    }
}

I initially ran into an issue using the Newtonsoft deserialzier rather than the MS one:

var jsonDictionary = await Task.Factory.StartNew(() => { return JsonConvert.DeserializeObject<Dictionary<string, object>>(json); });

Returned a Dictionary<string, object> but the factory couldn't make proper use of it.

You can use the Core2User or Core2EnterpriseUser classes if you're using v2 of the SCIM spec.

Furthermore the library, I believe can handle the creation of requests if you want (rather than crafting them yourself which does seem to be pretty straightforward anyway), here's another snippet from the author of the project (Craig McMurtry)

/* 
 * SampleProvider() is included in the Service library.  
 * Its SampleResource property provides a 2.0 enterprise user with values
 * set according to the samples in the 2.0 schema specification.
 */
var resource = new SampleProvider().SampleResource; 

// ComposePostRequest() is an extension method on the Resource class provided by the Protocols library. 
request = resource.ComposePostRequest("http://localhost:9000"); 

I hope this all helps, a massive amount of thanks are due to Craig McMurtry at Microsoft who has been very helpful in trying to get me up and running with the library - so I don't have to hand craft all my own model classes.

Please evaluate the open source project, https://github.com/PowerDMS/Owin.Scim. I've been leading this development effort. While it's missing a few features that Microsoft has implemented quite well, it is far more complete in most other areas of scim. See if it fits your needs and we welcome all feedback to help shape the future of owin.scim.

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