0

ユーザー名とパスワードのフィールドだけの、ごく普通のサインインページがあります。

<h2>Sign in</h2>

@Html.ValidationSummary()

@using (Html.BeginForm("SignIn"))
{
    <p>
        @Html.LabelFor(model => model.Username)
        @Html.TextBoxFor(model => model.Username)
    </p>
    <p>
        @Html.LabelFor(model => model.Password)
        @Html.PasswordFor(model => model.Password)
    </p>
    <input type="submit" value="Submit"/>
}

以下のように定義されたサインイン アクションがあります。

[HttpPost]
[ActionName("SignIn")]
public ActionResult SignInConfirmation(UserCredentialsModel model)
{
    if (ModelState.IsValid)
    {
        var userIsValid = Membership.ValidateUser(model.Username, model.Password);

        if (userIsValid)
        {
            FormsAuthentication.SetAuthCookie(model.Username, false);

            return RedirectToAction("Index", "Home");
        }
        ModelState.AddModelError(string.Empty, "Incorrect username and\\or password.");
    }

    return View();
}   

これに加えて、認証されているかどうかに応じて、「サインインしてください」または「ようこそユーザー」を表示するパーシャルがあります。

コードのデバッグと Fiddler 経由の両方で、Cookie が作成されて返されていることがわかります。

ただし、パーシャルがヒットすると、ユーザーは認証されません。

[ChildActionOnly]
public ActionResult Index()
{
    if (User.Identity.IsAuthenticated)  // =< This is always false.
    {
        var userDetailsModel = new UserDetailsModel
        {
            UserName = User.Identity.Name
        };

        return PartialView("Authenticated", userDetailsModel);
    }
    return PartialView("Unauthenticated");
}

私も手で転がしてみましFormsAuthenticationTicketた:

var userData = JsonConvert.SerializeObject(new { model.Username });
var issueDate = DateTime.Now;
var authenticationTicket = new FormsAuthenticationTicket(1,
    model.Username,
    issueDate,
    issueDate.AddMinutes(5),
    false,
    userData);

でも同じ結果…

認証されないのはなぜですか?

4

1 に答える 1

1

<authentication mode="Forms">web.configに次のようなものがありますか?

于 2015-09-15T12:59:58.253 に答える