3.2 Create创建用户

[HttpPost, Authorize]
public async Task<ActionResult> Create(
[Bind(Include = "UserPrincipalName,AccountEnabled,PasswordProfile,MailNickname,DisplayName,GivenName,Surname,JobTitle,Department")]
User user)
{
ActiveDirectoryClient client = null;
try
{
client = AuthenticationHelper.GetActiveDirectoryClient();
await client.Users.AddUserAsync(user);
}
catch (Exception e)
{
if (e.Message == "Authorization Required.")
{
HttpContext.GetOwinContext().Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
return View();
}
return RedirectToAction("Index");
}

Create的代码同样很清晰,采用了Bind Include简化了View的表单提交。对应的View的代码如下

@model Microsoft.Azure.ActiveDirectory.GraphClient.User

@{
ViewBag.Title = "CreateUser";
} <h2>Create User</h2> @using (Html.BeginForm("Create", "AzureActiveDirectory", null, FormMethod.Post, new { @class = "form-horizontal" }))
{
@Html.ValidationSummary(true) <div class="form-group">
@Html.LabelFor(model => model.UserPrincipalName, "用户名(英文名@域名)", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(model => model.UserPrincipalName)
@Html.ValidationMessageFor(model => model.UserPrincipalName)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.AccountEnabled, "账号启用", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.AccountEnabled)
@Html.ValidationMessageFor(model => model.AccountEnabled)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.PasswordProfile.Password, "密码(必须强密码)", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(model => model.PasswordProfile.Password)
@Html.ValidationMessageFor(model => model.PasswordProfile.Password)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.MailNickname, "别名(必须英文名)", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(model => model.MailNickname)
@Html.ValidationMessageFor(model => model.MailNickname)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.DisplayName, "显示名称", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(model => model.DisplayName)
@Html.ValidationMessageFor(model => model.DisplayName)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.GivenName, "名字", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(model => model.GivenName)
@Html.ValidationMessageFor(model => model.GivenName)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.Surname, "姓氏", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(model => model.Surname)
@Html.ValidationMessageFor(model => model.Surname)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.JobTitle, "职务", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(model => model.JobTitle)
@Html.ValidationMessageFor(model => model.JobTitle)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.Department, "部门", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(model => model.Department)
@Html.ValidationMessageFor(model => model.Department)
</div>
</div>
<p>
<input type="submit" value="Create" class="btn btn-primary" />
</p>
}

执行后的结果为

创建成功跳转到Index页面

为了让大家清楚的理解User属性对应的Azure门户管理上的提示,我在View中比较详细的做了说明,下表可以更清晰的看到对应

属性名

门户对应

要求

UserPrincipalName

用户名

英文名@域名

AccountEnabled

账号状态

 

Password

密码

必须强密码

MailNickname

别名

必须英文名

DisplayName

显示名称

 

GivenName

名字

 

Surname

姓氏

 

JobTitle

职务

 

Department

部门

 

StreetAddress

街道地址

 

City

城市

 

State

省/自治区/直辖市

 

Country

国家或地区

 

PhysicalDeliveryOfficeName

办公室号码

 

TelephoneNumber

办公电话

 

PostalCode

邮政编码

 

无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.2 Create创建用户]的更多相关文章

  1. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息]

    三.使用Azure AD管理用户信息 在上一章我们采用OpenID的方案和Azure AD交互进行身份验证,本章节我们继续了解如何在Azure AD中创建用户,列出用户信息,修改用户信息和删除用户信息 ...

  2. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.4 Edit修改用户信息]

    3.4 Edit修改用户信息 我们用FormCollection简化了表单提交,非常方便的进行用户信息修改. [HttpPost, Authorize] public async Task<Ac ...

  3. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.3 Details用户详细信息]

    3.3 Details用户详细信息 用户详细信息是通过objectId获取.代码如下 public async Task<ActionResult> Details(string obje ...

  4. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.1 Index用户列表]

    3.1 Index用户列表 或许当前域下的用户列表 [Authorize] public async Task<ActionResult> Index() { var userList = ...

  5. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.5 Delete删除用户]

    3.5 Delete删除用户 删除也是通过ObjectID获得对象进行删除 [Authorize] public async Task<ActionResult> Delete(strin ...

  6. 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证-2.2身份验证开发]

    2.2身份验证开发 在我们的案例中,我们是用户通过Web应用程序进行身份识别. 上面的图示说明了如下的一些概念 l Azure AD 是标识提供程序,负责对组织的目录中存在的用户和应用程序的标识进行验 ...

  7. 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证]

    二.使用Azure AD进行身份验证 之所以将Azure AD 作为开始,是应为基本上我们所有应用都需要进行安全管理.Azure Active Directory (Azure AD) 通过以下方式简 ...

  8. 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证--2.1使用Azure AD需要了解几个概念]

    2.1使用Azure AD需要了解几个概念 l Azure AD目录 当你注册 Microsoft 云服务时,便会获得一个 Azure AD 目录.你可根据需要创建更多的目录.例如,可以将第一个目录保 ...

  9. 无责任Windows Azure SDK .NET开发入门篇(一):开发前准备工作

    Windows Azure开发前准备工作 什么是 Azure SDK for .NET?微软官方告诉我们:Azure SDK for .NET 是一套应用程序,其中包括 Visual Studio 工 ...

随机推荐

  1. UVa10603 Fill

    解题思路:这是神奇的一题,一定要好好体会.见代码: #include<cstdio> #include<cstring> #include<algorithm> # ...

  2. HDU3232 Crossing rivers

    思路:这题关键一点就是根据题目的描述和测试数据得到启发,船都是 从对岸划过来的.心中有具体场景,就可以很简单了. #include<cstdio> int main() { ; ; whi ...

  3. HDU 5038 Grade

    解题思路:这题最关键的是要读懂题意,If not all the value are the same but the frequencies of them are the same, there ...

  4. mysql添加索引

    1.添加PRIMARY KEY(主键索引)  mysql>ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 2.添加UNIQUE(唯一索 ...

  5. Android 从java字节码告诉你 为什么Handler会造成内存泄露

    很多人面试的时候,都知道Handler 极易造成内存泄露,但是有一些讲不出来为什么,好一点的 会告诉你looper msg 之类的,但是你再往下问 为什么msg持有handler handler为什么 ...

  6. Amarino例程无法使用的问题

    Serial.begin(9600); 而不是用它的57600

  7. tilecache2.11在windows apache2.22安装部署

    tilecache2.11在windows apache2.22安装部署 蔡建良 2013-09-03 一.安装环境 操作系统: Windows7 32位 Apache2.22 Python2.5 m ...

  8. Linux下信号的简单使用

    1,1个main, 包含2个while, 不要被两个while中的sleep所迷惑,这里只有main()这一个主线程(进程)在运行,程序会按照自上而下顺序执行. 遇到第1个while循环中的sleep ...

  9. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File

    作为一个完成的应用程序,数据存储操作是必不可少的.因此,Android系统一共提供了四种数据存储方式.分别 是:SharePreference.SQLite.Content Provider和File ...

  10. 转载:50个C/C++源代码网站

    来源:http://www.cnblogs.com/feisky/archive/2010/03/05/1679160.html C/C++是最主要的编程语言.这里列出了50名优秀网站和网页清单,这些 ...