無責任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的代碼以下async

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

執行後的結果爲ui

1

建立成功跳轉到Index頁面編碼

3

爲了讓你們清楚的理解User屬性對應的Azure門戶管理上的提示,我在View中比較詳細的作了說明,下表能夠更清晰的看到對應 spa

屬性名code

門戶對應orm

要求blog

UserPrincipalNameip

用戶名ci

英文名@域名get

AccountEnabled

帳號狀態

 

Password

密碼

必須強密碼

MailNickname

別名

必須英文名

DisplayName

顯示名稱

 

GivenName

名字

 

Surname

姓氏

 

JobTitle

職務

 

Department

部門

 

StreetAddress

街道地址

 

City

城市

 

State

省/自治區/直轄市

 

Country

國家或地區

 

PhysicalDeliveryOfficeName

辦公室號碼

 

TelephoneNumber

辦公電話

 

PostalCode

郵政編碼

相關文章
相關標籤/搜索