Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have developed application in MVC 4 and EF with WCF I have created menu into _Layout page so now I am want to login with valid user so that I can authorize the access but I am not able to use Websecurity.Login it always show me false..

second thing I didn't some other scenario with cookies but once I tried to logoff it will not working..

means once I logoff and press Back button then it will redirect to as login I don't know why..?


is there any other way to change this menu structure or login menthod..


how I can make login proof access..


my login method is like this.


C#
myChannelFactory = new ChannelFactory<IBuilderTrackerServices>(myBinding, myEndpoint);
//Create a channel.
IBuilderTrackerServices wcfClientProperty = myChannelFactory.CreateChannel();
var modelResult = wcfClientProperty.AuthenticUser(model.UserName, model.Password);
((IClientChannel)wcfClientProperty).Close();
if (modelResult != null)
{
    var authTicket = new FormsAuthenticationTicket(
        1,
        model.UserName,
        DateTime.Now,
        DateTime.Now.AddMinutes(20),
        false,
        model.Id.ToString()
        );

    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
    var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
    HttpContext.Response.Cookies.Add(authCookie);
    return RedirectToAction("Index", "Home");
}
return View("~/Views/Account/Login.cshtml", model);



and my logoff method is like this..


C#
Session.Abandon();
FormsAuthentication.SignOut();
Response.Cookies.Clear();
Response.Expires = 0;
Response.Cache.SetNoStore();
Response.AppendHeader("Pragma", "no-cache");

// clear authentication cookie
HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
cookie1.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie1);

// clear session cookie (not necessary for your current problem but i would recommend you do it anyway)
HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
cookie2.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie2);
return Redirect("~/");



earlier my login method is like below but it always show me false.
so I had tried with another approach but still I am not happy with this.
this is my websecurity.login.

C#
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
    return RedirectToLocal(returnUrl);
}

// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);


my menu structure is like into the _Layout.cshtml


C#
@if (User.Identity.Name != string.Empty)
{
<nav>
    <ul class="sf-menu" id="example">
        <li>@Html.ActionLink("Home", "Index", "Dashboard")</li>

        <li class="current">
            <a href="#">Property</a>
            <ul>
                <li>@Html.ActionLink("Add", "Create", "AddProperty")</li>
                <li>@Html.ActionLink("Search", "Create", "SearchProperty")</li>
                <li>@Html.ActionLink("Inquiry", "Create", "MatchingInquiries")</li>
            </ul>
        </li>

                            </nav>
}


Thank you.
Posted

1 solution

When you are doing LogOut clear session or cookie you used. And when you LogIn check the Session,If Session is not created than users cant log to page even on clicking back button. But if you have not cleared your session properly during LogOut than it will go back
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900