简体   繁体   English

创建传入用户对象的用户 (Microsoft Graph)

[英]Create a user passing in a user object (Microsoft Graph)

I'm trying to create a user using a microsoft graph call using this link: https://docs.microsoft.com/en-us/graph/api/user-post-users?view=graph-rest-1.0&tabs=csharp我正在尝试使用以下链接使用 Microsoft 图形调用创建用户: https : //docs.microsoft.com/en-us/graph/api/user-post-users? view=graph-rest-1.0 &tabs=夏普

However, is there a way to create a user by passing in a User object or a json representation of a user object in an azure function as a parameter As shown below?但是,有没有办法通过在 azure 函数中传入 User 对象或用户对象的 json 表示作为参数来创建用户,如下所示?

[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "user/settings")] HttpRequest req, ILogger logger, User user) [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "user/settings")] HttpRequest req, ILogger logger, User user)

Also, is there a way to read the whole req.Body (w/ additional properties) and create a user, instead of only applying the required parameters (shown below)?另外,有没有办法读取整个 req.Body(带有附加属性)并创建用户,而不是仅应用所需的参数(如下所示)? Each and every user will have different properties每个用户都有不同的属性

{
    AccountEnabled = true,
    DisplayName = "displayName-value",
    MailNickname = "mailNickname-value",
    UserPrincipalName = "upn-value@tenant-value.onmicrosoft.com",
    PasswordProfile = new PasswordProfile
    {
        ForceChangePasswordNextSignIn = true,
        Password = "password-value"
    }
};
await graphClient.Users
    .Request()
    .AddAsync(user);

For your first question, I don't think we can pass a User object to function directly to create the user.对于你的第一个问题,我认为我们不能直接传递 User 对象来创建用户。 But we can pass all of the properties into the function to create it.但是我们可以将所有属性传递给函数来创建它。 As your second question mentioned, it is not easy for us to create the user if every user has different properties.正如你提到的第二个问题,如果每个用户都有不同的属性,我们很难创建用户。 So we can pass all of the properties as json into function and then read the json from the req and concat these properties as json string.所以我们可以将所有属性作为 json 传递给函数,然后从req读取 json 并将这些属性连接为 json 字符串。 After that, do not use graph sdk to create user.之后,不要使用图形 sdk 创建用户。 Do a post request with graph api by ourself like below code:像下面的代码一样,我们自己用图形 API 做一个 post 请求:

string str = "{\"displayName":\"xxxx\",\"mailNickname\":\"xxxx\"}";

var content = new StringContent(str, Encoding.UTF8, "application/json");

HttpClient client = new HttpClient();

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "your token");
var response = client.PostAsync("https://graph.microsoft.com/v1.0/users", content).Result;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM