我想注销时登录状态不起作用.它保持注销状态



可能的重复项:
登录状态不起作用,而我想注销时仍然保持注销状态

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. 

在这里,我给出了一些与登录和注销相关的代码:

在我的控制器文件夹中,我创建了商店管理器控制器.cs我在其中使用[Authorize(Roles = "Administrator"(] 在命名空间 MvcBookStore.Controllers 内。格式如下:

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 网站时自动添加的。我没有改变任何东西。我想在Visual Studio 2010上使用LoginStatus工具。因为它会自动找到注销网址。这就是为什么我把它添加到我的视图/商店经理/索引中.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>

一切都很好,但是当我单击注销链接时,它什么也没做,只是刷新自己。

您正在尝试将 WebForms 服务器控件与 MVC 混合 ASP.Net。
别这样;它行不通。

请改用 MVC 操作和逻辑。

相关内容

最新更新