如何隐藏登录用户的登录字段 [英] How to hide Login fields from the logged user

查看:72
本文介绍了如何隐藏登录用户的登录字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ADO.net开发ASP MVC.我正在尝试在用户登录后隐藏登录字段.

I am developping an ASP MVC using ADO.net. I am trying to hide the login fields after the user login on.

实际上,从主页进行身份验证后,用户将被重定向到他的配置文件页面.问题在于,当用户返回首页时,他总是会找到登录字段.

Indeed, after authentication from the home page, the user is redirected to his profil page. The problem is that when the user go back to the home page, he find always the Login Fields.

根据调试器,Session ["currentUser"]始终保持为空,然后呈现脚本.

According to the Debugger, Session["currentUser"] stay always null and then the script is rendered.

在开发工具中我也没有发现错误.

I didn't find also an error in dev tools.

这是我尝试过的:

 @if (Session["currentUser"] != null)
 {
    <script type='text/javascript'>
$(document).ready(function(){
    $("#login").hide();
});
    </script>
 }
        <div class="login" id="login">
            @*@RenderPage("~/Views/Home/Login.cshtml")*@
            <link href="~/Content/toastr.css" rel="stylesheet" />
            <div class="main-w3l">
                <div class="w3layouts-main" style="background-image:url('/template/web/images/bg3.jpg'); margin-top:50px;">
                    <h2>Login Now</h2>


                    @using (Html.BeginForm("Login", "Home", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                    {


                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)

                        <input value="E-MAIL" name="Email" type="email" required="" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'E-Mail';}" />
                        <input value="PASSWORD" name="Password" type="password" required="" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'password';}" />
                        <span><input type="checkbox" />Remember Me</span>
                        <h6><a href="#">Forgot Password?</a></h6>
                        <div class="clear"></div>
                        <input type="submit" value="login" name="login">

                    }

                    <p>Don't Have an Account ?<a href="#" onclick="@("window.location.href='" + @Url.Action("Create", "Client") + "'") ;">Register Now</a></p>
                </div>
            </div>

更新:控制器:

  public ActionResult Login()
        {
            return View();
        }

    database_Access_layer.db dblayer = new database_Access_layer.db();


    [HttpPost]
    public ActionResult Login(FormCollection fc, string LastName, string Email)
    {

        int res = dblayer.Admin_Login(fc["Email"], fc["Password"]);
        if (res == 1)
        {
            Session["currentUser"] = Email;
            string z = Email;
            connection();
            con.Open();
            SqlCommand command = new SqlCommand("select Email from Client", con);

            List<string> result = new List<string>();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    result.Add(reader.GetString(0));
                con.Close();
            }
            foreach (string x in result)
            {
                if (x == z)
                {

                    SqlCommand command2 = new SqlCommand($"select LastName from Client WHERE Email= '{x}' ", con);
                    con.Open();
                    string y = command2.ExecuteScalar().ToString();
                    con.Close();
                    Session["currentUser"] = y;

                }
            }

            return RedirectToAction("Profil", "Client");
            Session.RemoveAll();


        }
        else {

            TempData["msg"] = " Email or Password is wrong !";
            return RedirectToAction("Index", "Home");

        }

    }

推荐答案

内置的系统可以帮助您进行登录.出于多种原因,不应使用会话变量.

There are built in systems to help you with the login. Session variables shouldn't be used for several reasons.

要设置登录名,您可以在控制器内部使用以下功能:

To set the login, you can use the following function inside your controller:

FormsAuthentication.SetAuthCookie(UserName, remember);

类似地,您可以使用 FormsAuthentication.SignOut()登出.

Similarly you can use FormsAuthentication.SignOut() for logging out.

现在在剃须刀中或任何控制器中,您可以检查 User.Identity.IsAuthenticated 以验证用户是否已登录.

Now within your razor or in any controller you can check for User.Identity.IsAuthenticated to verify if the user is logged in.

要使用此功能,还需要确保在web.config下的< system.web> 下具有以下内容:

For this functionality to work, you also need to make sure you have the following under your web.config, under your <system.web>:

<authentication mode="Forms">

</authentication>

此外,如果您想利用此登录功能,可以将登录表单添加到与首页不同的单独的cshtml页面中.可以说此页面位于 Home/Login 的位置.现在,您可以在web.config中修改上述代码,如下所示

Further more if you would like to take advantage of this login feature, you can add your login form into a separate cshtml page different from your home page. Lets say this page is in the location Home/Login. Now you can modify the above code within web.config as follows

<authentication mode="Forms">
    <forms loginUrl="~/Home/Login" timeout="2880" />
</authentication>

这将分配您的默认登录页面.现在,如果仅在登录时需要访问任何URL,如果用户未登录,它将自动重定向到该位置.要指定某个操作需要身份验证,请在该操作之前使用 [Authorize] 或整个控制器(如果需要整个控制器的授权).您的操作将如下所示:

This assigns your default login page. Now if any URL needs to only be accessed on login, it would automatically redirect to this location if the user is not logged in. To specify that an action needs authentication, use [Authorize] before the action or the entire controller if you need the whole controller authorized. Your action will look something like this:

[Authorize]
public ActionResult Index(){
    return View()
}

最后,您的示例 HomeController 看起来可能像这样:

Finally your sample HomeController can look something like this:

public class HomeController : Controller
{

    public ActionResult Index()
    {
        return View();
    }

    [HttpGet]
    public ActionResult Login()
    {
        return View();  // for the login form
    }

    [HttpPost]
    public void Login(string UserName, .... <other fields>)
    {
        // validate your login first here
        FormsAuthentication.SetAuthCookie(UserName, true);
    }

}

这篇关于如何隐藏登录用户的登录字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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