かみそりのフォームを含むページで Web サイトを作成しました。ユーザーはこのフォームにログインしてから、別のページにリダイレクトできます。ログイン (およびログアウト) は、フォーム認証で正常に機能します。ただし、HttpContext.Current.User.Identity.Name を使用して (フォーム認証 Cookie に) 保存されているユーザー名を取得できないようです。空の文字列 "" を返します。
標準メンバーシップまたはロール プロバイダーなしで MVC 5 および ASP 4.5 を使用しています。
ログイン:
[HttpPost]
public ActionResult Login(User user)
{
if (ModelState.IsValid)
{
bool authenticated = userscontroller.isAuthorized(user.Email, user.Password);
if (authenticated)
{
if (userscontroller.isAuthenticated())
{
userscontroller.deAuthenticateUser();
}
userscontroller.authenticateUser(user);
return Redirect(Url.Action("Index", "Home"));
}
}
}
ユーザーの認証:
public void authenticateUser(User user)
{
FormsAuthentication.SetAuthCookie(user.Username, false);
}
次に、ユーザーの名前を取得します。
public User userFromCookie()
{
if (isAuthenticated())
{
return getUserByUsername(HttpContext.Current.User.Identity.Name);
}
else { return null; }
}
isauthenticated()
public bool isAuthenticated()
{
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
return true;
}
else
{
return false;
}
}
ウェブ構成:
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<authorization > <deny users="?"/> </authorization>
したがって、identity.name は "" を返します。
助けていただければ幸いです。