FormsAuthentication
のログインとログアウトの役割を設定しようとしています。 @User.Identity.Name
は、これらの設定を使用してWindowsドメインユーザー名を返すのはなぜですか?Windows認証の問題
<authentication mode="Forms">
<forms name="Login" loginUrl="~/User/Login" defaultUrl="~/Home/Index" protection="All" timeout="90" slidingExpiration="true"/>
</authentication>
VS2015プロジェクトプロパティ - >開発サーバーでは、Windows認証が[無効]に設定されています。
結果として、フォーム名が@User.Identity.Name
で表示されることがありますが、ほとんどの場合、ログインまたはログアウトするとWindowsドメイン名が取得されます。私は@User.Identity.Name
にuser.UserName
を入れているはずです(正しく表示されているかどうか分かります)。
UserControllerでは、私はでFormsAuthenticationTicket
を設定しました。ログアウト時
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user.UserName, DateTime.Now, DateTime.Now.AddMinutes(90), user.RememberMe, user.Roles, FormsAuthentication.FormsCookiePath);
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
if (ticket.IsPersistent)
{
cookie.Expires = ticket.Expiration;
}
Response.Cookies.Add(cookie);
FormsAuthentication.RedirectFromLoginPage(user.UserName, user.RememberMe);
、私は空にするために同じクッキーを設定し、Session.Abandon()
。私はあらゆる種類のものを試してみました
@if (User != null)
{
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">
Logged in as @User.Identity.Name
<span class="caret"></span>
</a>
が、私はここに私の髪の毛を抜くことだし、それは私が間違っているつもりだということであるかを理解することはできません。
FormsAuthentication.SignOut();
Session.Abandon();
HttpContext.User = new GenericPrincipal(new GenericIdentity(string.Empty), null);
// clear authentication cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, "");
cookie.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie);
return RedirectToAction("Login", "User");
は、これは私がユーザ名を表示する方法です。
ユーザー名を取得して表示するコードを表示できますか? –
@YvetteColomb質問に追加されました。 web.configにページを管理者ロールに限定する設定があるため、クッキーがクリアされたりオーバーライドされているとは思われません。ユーザーは常にログインしているため、解雇されていません。ゲスト状態(ログアウトするとWindows認証とドメイン名が表示されます)またはWindows認証ではなく「user.UserName」を使用します。それがそれをより良く説明することを望みます。 – kamil1995b