0

重複の可能性:
ログアウトしたいときにログインステータスが機能しない ログアウトのまま

I am studding a tutorial on asp.net mvc which name is MvcMusicStore. I am creating MvcBookStore For login and logout i use the facility of ASP.net configuration. I am successful in login but unsuccessful in logout using loginstatus from toolbox. 

ここで、ログインとログアウトに関連するコードをいくつか示します。

コントローラーフォルダーで、名前空間 MvcBookStore.Controllers 内で [Authorize(Roles = "Administrator")] を使用する StoreManagerController.cs を作成しました。フォーマットは以下のとおりです。

namespace MvcBookStore.Controllers
{
[Authorize(Roles = "Administrator")]
public class StoreManagerController : Controller
  {
       // code goes here...
  }  
} 

モデル フォルダーに AccountModel.cs ファイルを作成し、以下に必要なコードを示します。

namespace MvcBookStore.Models
{
    #region Services
public interface IFormsAuthenticationService
{
    void SignIn(string userName, bool createPersistentCookie);
    void SignOut();
}

public class FormsAuthenticationService : IFormsAuthenticationService
{
    public void SignIn(string userName, bool createPersistentCookie)
    {
        ValidationUtil.ValidateRequiredStringValue(userName, "userName");

        FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
    }

    public void SignOut()
    {
        FormsAuthentication.SignOut();
    }
}

上記のコードは、asp.net mvc Web サイトが作成されると自動的に追加されます。私は何も変えていません。Visual Studio 2010 で LoginStatus 機能を使用したい。ログアウトURLを自動的に見つけるためです。これが、View/StoreManager/Index.aspx の位置に追加する理由です。コードを以下に示します。

    <%@ Page Title="" Language="C#"  AutoEventWireup ="true" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcBookStore.Models.Album>>" %>
<script runat="server">
protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
    {
       FormsAuthentication.SignOut();
       Response.Redirect("/View/Home/Index.aspx");    
    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <form id="form1" runat="server">

    <h2>Index&nbsp;
        <asp:LoginStatus ID="LoginStatus1" runat="server" 
            onloggingout="LoginStatus1_LoggingOut" 
            LogoutAction="Redirect" />
    </h2>

    <table>
        <tr>
            <th></th>
            <th>Title</th>
            <th>Artist</th>
            <th>Genre</th>
        </tr>

    <% foreach (var item in Model) { %>
        <tr>
            <td>
                <%: Html.ActionLink("Edit", "Edit", new { id=item.AlbumId }) %> |
                <%: Html.ActionLink("Delete", "Delete", new { id=item.AlbumId })%>
            </td>
            <td><%: Html.Truncate(item.Title, 25) %></td>
            <td><%: Html.Truncate(item.Artist.Name, 25) %></td>
            <td><%: item.Genre.Name %></td>
        </tr>

    <% } %>

    </table>

    <p>
        <%: Html.ActionLink("Create New", "Create") %>

    </p>
    <p>
          <a href="/Control/ordercontrol.aspx" > control order</a>

    </p>

    </form>

</asp:Content>

すべて問題ありませんが、ログアウトリンクをクリックしても何も更新されません。

4

1 に答える 1

0

ASP.Net WebForms サーバー コントロールと MVC を混在させようとしています。
そうしないでください。うまくいきません。

代わりに、MVC アクションとロジックを使用してください。

于 2012-05-03T17:22:47.047 に答える