Azure active directory 如何使用graph api 1.5在azure active directory中为用户分配应用程序角色?
在azure active directory中添加用户应用程序角色时,我遇到了一个问题。 在用户模型中,有一个名为Approvalinsignment的属性,我想我可以在其中为用户设置应用程序角色。但当我这样做时,它并没有分配。有人能帮我吗 我在应用程序模型中使用应用程序角色添加了应用程序角色。在创建用户时,我可以通过下拉列表进行填充。下面显示了创建用户部分。 在Approvalization中,什么应该作为ResourceId提供Azure active directory 如何使用graph api 1.5在azure active directory中为用户分配应用程序角色?,azure-active-directory,Azure Active Directory,在azure active directory中添加用户应用程序角色时,我遇到了一个问题。 在用户模型中,有一个名为Approvalinsignment的属性,我想我可以在其中为用户设置应用程序角色。但当我这样做时,它并没有分配。有人能帮我吗 我在应用程序模型中使用应用程序角色添加了应用程序角色。在创建用户时,我可以通过下拉列表进行填充。下面显示了创建用户部分。 在Approvalization中,什么应该作为ResourceId提供 [HttpPost] public asy
[HttpPost]
public async Task<ActionResult> Create(UserModel user)
{
ActiveDirectoryClient client = null;
try
{
client = AuthenticationHelper.GetActiveDirectoryClient();
}
catch (Exception e)
{
if (Request.QueryString["reauth"] == "True")
{
//
// Send an OpenID Connect sign-in request to get a new set of tokens.
// If the user still has a valid session with Azure AD, they will not be prompted for their credentials.
// The OpenID Connect middleware will return to this controller after the sign-in response has been handled.
//
HttpContext.GetOwinContext()
.Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
//
// The user needs to re-authorize. Show them a message to that effect.
//
ViewBag.ErrorMessage = "AuthorizationRequired";
return View();
}
try
{
User mappedUser = MapToUser(user);
await client.Users.AddUserAsync(mappedUser);
await AddUserRole(mappedUser, user);
return RedirectToAction("Index");
}
catch (Exception exception)
{
ModelState.AddModelError("", exception.Message);
return View();
}
}
/// <summary>
/// Adds the user role.
/// </summary>
/// <param name="mappedUser">The mapped user.</param>
/// <param name="model">The model.</param>
/// <returns></returns>
private async Task AddUserRole(User mappedUser, UserModel model)
{
ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient();
IPagedCollection<IUser> pagedCollection = await client.Users.ExecuteAsync();
var currentUser = pagedCollection.CurrentPage.Where(x => x.UserPrincipalName.Equals(mappedUser.UserPrincipalName)).FirstOrDefault();
var appRoleAssignment = new AppRoleAssignment();
appRoleAssignment.Id = model.AppRoleId;
appRoleAssignment.PrincipalId = Guid.Parse(currentUser.ObjectId);
appRoleAssignment.ResourceId = Guid.Parse(clientId);
////((ClaimsIdentity)ClaimsPrincipal.Current.Identity).AddClaim(
//// new Claim(ClaimTypes.Role, "Manager", ClaimValueTypes.String, "GRAPH"));
await currentUser.UpdateAsync();
////Remaining have to be completed.
}
/// <summary>
/// Maps to user.
/// </summary>
/// <param name="model">The model.</param>
/// <returns></returns>
private Microsoft.Azure.ActiveDirectory.GraphClient.User MapToUser(UserModel model)
{
var user = new User();
user.UserPrincipalName = model.UserPrincipalName;
user.AccountEnabled = model.AccountEnabled;
user.PasswordProfile = model.PasswordProfile;
user.MailNickname = model.MailNickname;
user.DisplayName = model.DisplayName;
user.GivenName = model.GivenName;
user.Surname = model.Surname;
user.JobTitle = model.JobTitle;
user.Department = model.Department;
return user;
}
[HttpPost]
公共异步任务创建(UserModel用户)
{
ActiveDirectoryClient=null;
尝试
{
client=AuthenticationHelper.GetActiveDirectoryClient();
}
捕获(例外e)
{
if(Request.QueryString[“reauth”]=“True”)
{
//
//发送OpenID连接登录请求以获取一组新令牌。
//如果用户仍然拥有与Azure AD的有效会话,则不会提示他们输入凭据。
//处理登录响应后,OpenID Connect中间件将返回到此控制器。
//
HttpContext.GetOwinContext()
.Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
//
//用户需要重新授权。向他们显示一条这样的消息。
//
ViewBag.ErrorMessage=“需要授权”;
返回视图();
}
尝试
{
用户mappedUser=MapToUser(用户);
等待client.Users.AddUserAsync(mappedUser);
等待AddUserRole(mappedUser,用户);
返回操作(“索引”);
}
捕获(异常)
{
ModelState.AddModelError(“,exception.Message”);
返回视图();
}
}
///
///添加用户角色。
///
///映射的用户。
///模型。
///
专用异步任务AddUserRole(用户mappedUser,用户模型)
{
ActiveDirectoryClient=AuthenticationHelper.GetActiveDirectoryClient();
IPagedCollection pagedCollection=await client.Users.ExecuteAsync();
var currentUser=pagedCollection.CurrentPage.Where(x=>x.UserPrincipalName.Equals(mappedUser.UserPrincipalName)).FirstOrDefault();
var ApprovalAssignment=新ApprovalAssignment();
approvalsignment.Id=model.approvleId;
Approvalsignment.PrincipalId=Guid.Parse(currentUser.ObjectId);
approvalsignment.ResourceId=Guid.Parse(clientId);
////((ClaimsIdentity)ClaimsPrincipal.Current.Identity).AddClaim(
////新声明(ClaimTypes.Role,“Manager”,ClaimValueTypes.String,“GRAPH”);
等待currentUser.UpdateSync();
////剩下的部分必须完成。
}
///
///映射到用户。
///
///模型。
///
私有Microsoft.Azure.ActiveDirectory.GraphClient.User MapToUser(用户模型)
{
var user=新用户();
user.UserPrincipalName=model.UserPrincipalName;
user.AccountEnabled=model.AccountEnabled;
user.PasswordProfile=model.PasswordProfile;
user.MailNickname=model.MailNickname;
user.DisplayName=model.DisplayName;
user.GivenName=model.GivenName;
user.name=model.name;
user.JobTitle=model.JobTitle;
user.Department=model.Department;
返回用户;
}
很抱歉,我很难理解您的代码,因为其中一些代码丢失了(比如您对应用程序的定义以及它声明的任何应用程序角色)。在高层,应用程序将定义一系列应用程序角色(approle)。一旦用户同意该应用程序,表示该应用程序的应用程序实例将出现在同意的租户中。如果要将用户分配给该应用程序,在其中一个指定的应用程序角色中,需要设置对该用户的批准(选择应用程序角色id和资源id-应用程序的id)。这里描述了Approvalization及其属性:以及这里描述的RESTAPI示例
此外,我们还有一个控制台示例,它显示了应用程序上的许可创建,以及应用程序(在声明的应用程序角色中)对用户的分配。您可以在github上找到:
HTHs,很抱歉,我很难理解您的代码,因为其中一些代码丢失了(比如您对应用程序的定义以及它声明的任何应用程序角色)。在高层,应用程序将定义一系列应用程序角色(approle)。一旦用户同意该应用程序,表示该应用程序的应用程序实例将出现在同意的租户中。如果要将用户分配给该应用程序,在其中一个指定的应用程序角色中,需要设置对该用户的批准(选择应用程序角色id和资源id-应用程序的id)。这里描述了Approvalization及其属性:以及这里描述的RESTAPI示例 此外,我们还有一个控制台示例,它显示了应用程序上的许可创建,以及应用程序(在声明的应用程序角色中)对用户的分配。您可以在github上找到:
HTHs,代码中的资源ID是错误的:
appRoleAssignment.ResourceId = Guid.Parse(clientId);
graph应用程序使用的资源是与应用程序关联的服务原则的ID。您可以阅读应用程序和服务原则之间的区别,但是bri
var servicePrincipleList = await client.ServicePrincipals.Where(e => e.AppId == clientId).ExecuteAsync();
var roleResourceUpdateID = servicePrincipleList.CurrentPage.First().ObjectId;