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.
myChannelFactory = new ChannelFactory<IBuilderTrackerServices>(myBinding, myEndpoint);
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..
Session.Abandon();
FormsAuthentication.SignOut();
Response.Cookies.Clear();
Response.Expires = 0;
Response.Cache.SetNoStore();
Response.AppendHeader("Pragma", "no-cache");
HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
cookie1.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie1);
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.
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
my menu structure is like into the _Layout.cshtml
@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.