3.4 Edit修改用户信息

我们用FormCollection简化了表单提交,非常方便的进行用户信息修改。

[HttpPost, Authorize]
public async Task<ActionResult> Edit(User user, FormCollection values)
{
try
{
ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient();
IUser toUpdate = await client.Users.GetByObjectId(values[“ObjectId”]).ExecuteAsync();
foreach (var value in values)
{
var sourceProp = user.GetType().GetProperty(value.ToString(), BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
var destProp = toUpdate.GetType().GetProperty(value.ToString(), BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
if (null != sourceProp && sourceProp.CanRead)
{
if (null != destProp && destProp.CanWrite)
{
destProp.SetValue(toUpdate, sourceProp.GetValue(user, null), null);
}
}
}
await toUpdate.UpdateAsync();
return RedirectToAction("Index");
}
catch (Exception)
{
return View();
}
}

对应的View代码为

@model Microsoft.Azure.ActiveDirectory.GraphClient.User
@{
ViewBag.Title = "UserEdit";
} <h2>UserEdit</h2> @using (Html.BeginForm("Edit", "AzureActiveDirectory", null, FormMethod.Post, new { @class = "form-horizontal" }))
{
@Html.ValidationSummary(true) <div class="form-group">
@Html.Label("照片", new { @class = "col-sm-2 control-label" })
<input type=file name="photofile" class="col-sm-10" />
</div> <div class="form-group">
@Html.LabelFor(model => model.UserPrincipalName,"用户名(全名)", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.DisplayFor(model => model.UserPrincipalName)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.DisplayName,"显示名称", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.DisplayName)
@Html.ValidationMessageFor(model => model.DisplayName)
</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.GivenName,"名字", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(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.EditorFor(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.EditorFor(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.EditorFor(model => model.Department)
@Html.ValidationMessageFor(model => model.Department)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.Mobile, new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.Mobile)
@Html.ValidationMessageFor(model => model.Mobile)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.StreetAddress,"街道地址", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.StreetAddress)
@Html.ValidationMessageFor(model => model.StreetAddress)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.City,"城市", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.City)
@Html.ValidationMessageFor(model => model.City)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.State,"省/自治区/直辖市", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.State)
@Html.ValidationMessageFor(model => model.State)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.Country,"国家或地区", new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.EditorFor(model => model.Country)
@Html.ValidationMessageFor(model => model.Country)
</div>
</div>
<p>
<input type="submit" value="Edit" class="btn btn-primary" />
</p> }
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

成功运行后的效果为

返回Index,你会观察到职务列有了信息

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

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

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

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

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

  3. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.2 Create创建用户]

    3.2 Create创建用户 [HttpPost, Authorize] public async Task<ActionResult> Create( [Bind(Include = & ...

  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. ecms_任意页面调用单独的栏目

    <a href="<?=$class_r[58]['classpath']?>"> <?=$class_r[58]['classname']?> ...

  2. HDU 整除的尾数 2099

    解题思路:很简单的一道水题,这几天比较忙,没怎么刷题,找找自信,很快1A.   还可以,嘿嘿 #include<cstdio> #include<cstring> #inclu ...

  3. 【转】在Source Insight中看Python代码

    原文网址:http://www.cnblogs.com/xuxm2007/archive/2010/09/02/1815695.html SI是个很强大的代码查看修改工具,以前用来看C,C++都是相当 ...

  4. LruCache--远程图片获取与本地缓存

    Class Overview A cache that holds strong references to a limited number of values. Each time a value ...

  5. 企业网站DDOS防护解决方案

    随着网络的普及,越来越多的企业开始了上网之路,由于网络安全知识的欠缺,很多企业以为做一个网站就 等于 网络化了,于是狠花血本请专业网络公司制作出各种漂亮的网页.但做完才发现,网络上的各种漏洞的DDOS ...

  6. LR之配置端口映射(port mapping)

    1.那些协议需要配置 tools-recording_options-network-port mapping 2.定义端口映射 3.自动检测原理 4.特殊情况

  7. 一些JS周边工具

    Visual Studio JS 辅助插件 JScript Editor Extensions 功能: 1.     代码块折叠 2.     方法参数智能提示 3.     代码块Outlining ...

  8. NET知识大纲

    第一部分 C#编程基础 1.(30)变量.运算符(+.-.*./.++.--.括号.==.!=.>.<.>=.<=.&&.||).流程控制(if.while.f ...

  9. 关于WordPress建站的原理二三事

    在写关于仿站文章详情页如何制作之前,我觉得有必要就一些原理性的问题,做一些说明.文章详情页的核心模块和首页有很多相似的地方,比如调用文章的标题.文章的内容.文章分类.作者等,实现起来都差不多,因此,了 ...

  10. C#获取ftp文件最后修改时间

    public static DateTime GetFileModifyDateTime(string ftpServerIP,string ftpFolder,string ftpUserID,st ...