MVC 4使用EF& amp; WCF .. [英] MVC 4 Login with EF & WCF..

查看:108
本文介绍了MVC 4使用EF& amp; WCF ..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我已经在MVC 4和EF中使用WCF开发了应用程序我已经在_Layout页面中创建了菜单所以现在我想要使用有效用户登录以便我可以授权访问但是我我无法使用Websecurity.Login它总是显示我的错误..



第二件事我没有其他情况下使用cookies但是一旦我试图注销它就会不工作..



表示一旦我注销并按回按钮然后它将重定向到登录我不知道为什么..?





有没有其他方法可以更改此菜单结构或登录menthod ..





如何进行登录证明访问..





我的登录方法是这样的。





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);
//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..


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("~/");







之前我的登录方法如下所示,但它总是显示我的错误。

所以我尝试了另一种方法,但我对此并不满意。

这是我的websecurity.login。






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);
}

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





我的菜单结构就像_Layout.cshtml







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.

推荐答案

当你做 LogOut时清除您使用的会话或Cookie。当您登录检查会话时,如果未创建会话,则用户即使单击后退按钮也无法登录页面。但如果您在 LogOut 期间没有正确清理会话,那么它将会返回
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


这篇关于MVC 4使用EF&amp; amp; WCF ..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆