我正在使用Active Directory身份验证库来创建用户并列出它们。 我有一个视图和控制器动作来执行此操作。
但请查看视图的MODEL,它是来自Azure Active Directory API的用户实体
@model Microsoft.Azure.ActiveDirectory.GraphClient.User
@{
ViewBag.Title = "Create";
Layout = "~/Areas/GlobalAdmin/Views/Shared/_LayoutGlobalAdmin.cshtml";
}
<div class="row wrapper border-bottom white-bg page-heading">
<div class="col-sm-4">
<h2>Create</h2>
<ol class="breadcrumb">
<li>
@Html.ActionLink("List", "Index")
</li>
<li class="active">
<strong>Create</strong>
</li>
</ol>
</div>
<div class="col-sm-8">
<div class="title-action">
@Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-primary"})
</div>
</div>
</div>
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
<div class="col-lg-12">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>Crear Usuario</h5>
</div>
<div class="ibox-content">
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
<div class="editor-label">
@Html.LabelFor(model => model.UserPrincipalName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserPrincipalName)
@Html.ValidationMessageFor(model => model.UserPrincipalName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AccountEnabled)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AccountEnabled)
@Html.ValidationMessageFor(model => model.AccountEnabled)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PasswordProfile.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PasswordProfile.Password)
@Html.ValidationMessageFor(model => model.PasswordProfile.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MailNickname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MailNickname)
@Html.ValidationMessageFor(model => model.MailNickname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DisplayName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DisplayName)
@Html.ValidationMessageFor(model => model.DisplayName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.GivenName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.GivenName)
@Html.ValidationMessageFor(model => model.GivenName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Surname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Surname)
@Html.ValidationMessageFor(model => model.Surname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.JobTitle)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.JobTitle)
@Html.ValidationMessageFor(model => model.JobTitle)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Department)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Department)
@Html.ValidationMessageFor(model => model.Department)
</div>
<div class="editor-label">
@Html.Label("Empresa")
</div>
@{
List<SelectListItem> listItems = new List<SelectListItem>();
listItems.Add(new SelectListItem
{
Text = "Company1",
Value = "Company1"
});
listItems.Add(new SelectListItem
{
Text = "Company2",
Value = "Company2",
Selected = true
});
listItems.Add(new SelectListItem
{
Text = "Company3",
Value = "Company3"
});
}
<div class="editor-field">
@Html.DropDownListFor(model => model.Department)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
</div>
</div>
</div>
</div>
</div>
在Azure Active Directory中,与在AD上一样,您可以添加自定义属性,这些自定义属性称为azure活动目录架构扩展。这里有很好的解释:http://justazure.com/azure-active-directory-part-6-schema-extensions/
然而,这不是我所担心的问题。
我的Active Directory已经有一个名为company的新扩展属性。 我现在的代码设置该扩展属性的值,检查此控制器操作,待完成的行
public async Task<ActionResult> Create(
[Bind(
Include =
"UserPrincipalName,AccountEnabled,PasswordProfile,MailNickname,DisplayName,GivenName,Surname,JobTitle,Department"
)] Microsoft.Azure.ActiveDirectory.GraphClient.User 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
{
await client.Users.AddUserAsync(user);
//TO BE FINISHED
user.SetExtendedProperty("Compania", "");
await user.UpdateAsync();
Task.WaitAll();
// Save the extended property value to Azure AD.
user.GetContext().SaveChanges();
return RedirectToAction("Index");
}
catch (Exception exception)
{
ModelState.AddModelError("", exception.Message);
return View();
}
}
问题是:
DropDownListFor需要模型中的属性,但是在User类中没有属性所以如何创建下拉列表?在我的视图中,你可以看到有样本数据,我会照顾稍后用数据源中的实际数据修复它。
我修复后如何获取控制器中所选下拉列表的值?
答案 0 :(得分:1)
您始终可以将键值对列表属性添加到视图模型中。当您的用户向AD实体添加扩展属性时,您可以扩展该列表。当您传递到视图时,您的列表可以作为空集合开始。然后在您的视图中,遍历集合中的项目以在列表类型显示中显示每个键值对。
您的控制器可以使用一种方法来提供键值对条目的局部视图(即,带有两个输入的局部视图:一个用于属性名称,一个用于值),因此当用户单击按钮时您要添加新的扩展属性的视图,您可以调用该控制器方法来为您提供输入字段的渲染。