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


    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>
    }

    执行后的结果为

    1

    创建成功跳转到Index页面

    3

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

    属性名

    门户对应

    要求

    UserPrincipalName

    用户名

    英文名@域名

    AccountEnabled

    账号状态

     

    Password

    密码

    必须强密码

    MailNickname

    别名

    必须英文名

    DisplayName

    显示名称

     

    GivenName

    名字

     

    Surname

    姓氏

     

    JobTitle

    职务

     

    Department

    部门

     

    StreetAddress

    街道地址

     

    City

    城市

     

    State

    省/自治区/直辖市

     

    Country

    国家或地区

     

    PhysicalDeliveryOfficeName

    办公室号码

     

    TelephoneNumber

    办公电话

     

    PostalCode

    邮政编码

     

  • 相关阅读:
    人生苦短我学Java-1-Helloword
    python-51-MySQLdb查询返回dict格式
    IDEA/PyCharm等系列-会了这些设置编码舒服而效率又提高了一截
    jacoco-2-jenkins集成代码测试覆盖率
    jacoco-1-java代码测试覆盖率之本地环境初体验
    wordpress 安装提示 Error Establishing a Database Connection
    mysql 8.0 重置 root 账户密码
    Ubuntu 20.04 卸载 snapd
    VSCode 扩展选择快捷键插件 Quick and Simple Text Selection
    Swagger 响应数据 response 里包含动态变化的对象 key 的方法
  • 原文地址:https://www.cnblogs.com/shyleoking/p/4719040.html
一二三 - 开发者的网上家园