Search code examples
c#asp.net-mvcrazormembership-providerumbraco7

umbraco 7.1 logout (razor syntax C#)


I have an umbraco 7.1 website and I use its own membership system.I can easily log in,see my status,even change password BUT there is no logout command and I don't know what method I should use for umbraco 7 to logout current user.I know i can clear the cookie and member identity but it always gives a runtime error. Help me! :D


Solution

  • In the backoffice (Admin) you can add a new Partial View and choose the Login Status template and you will get the following code.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @using System.Web.Mvc.Html
    @using ClientDependency.Core.Mvc
    @using Umbraco.Web
    @using Umbraco.Web.Models
    @using Umbraco.Web.Controllers
    
    @{
        var loginStatusModel = Members.GetCurrentLoginStatus();
    
        Html.EnableClientValidation();
        Html.EnableUnobtrusiveJavaScript();
        Html.RequiresJs("/umbraco_client/ui/jquery.js");
        Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.min.js");
        Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.unobtrusive.min.js");
    
        var logoutModel = new PostRedirectModel();
    
    
        //Here you can specify a redirect URL for after logging out, by default umbraco will simply
        //redirect to the current page. Example to redirect to the home page:
    
        //logoutModel.RedirectUrl = "/"; 
    
    }   // NOTE: This RenderJsHere code should be put on your main template page where the rest   of your script tags are placed
    @Html.RenderJsHere()
    @if (loginStatusModel.IsLoggedIn)
    {
        <p>You are currently logged in as @loginStatusModel.Name</p>
    
       using (Html.BeginUmbracoForm<UmbLoginStatusController>("HandleLogout"))
       {
            <fieldset>
                <legend>Logout</legend>
                <button>Logout</button>
            </fieldset>
    
           @Html.HiddenFor(m => logoutModel.RedirectUrl)
       }
    }