将数据发送到我的母版页

时间:2015-10-06 08:05:14

标签: c# sql asp.net-mvc forms-authentication

我正在使用内部网(纠正错误和添加功能) - 但是管理员标签的显示存在问题,它会以任何方式显示!

我现在能够通过以下SQL请求知道Employee是否是管理员:

$rsa = new Crypt_RSA()

行为应该是:如果$rsa->loadKey(file_get_contents({my_key}))为真,则显示“管理”标签(或者只是拒绝其访问权限),如果不是,请不要。

但我有两个主要问题:

  • 我想知道在哪里可以填写我的$this->sftp->login()变量,无论如何填充它(这样我就不必登录以填充它)

  • 而且我不知道如何将它传递给我的[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(LoginModel model, string returnUrl) { // Variables string isAdministrator; bool isAdmin; // Checks the admin status of logging in user using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)) { using (SqlCommand command = new SqlCommand("SELECT isAdministrator FROM Employee WHERE Login = '" + model.UserName + "'", connection)) { connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { isAdministrator = reader[0].ToString(); isAdmin = Convert.ToBoolean(isAdministrator); ViewData["isAdmin"] = isAdmin; } } } } // Not relevant things related to logging in return View(model); }

1 个答案:

答案 0 :(得分:1)

Use for example ViewBag.IsAdmin = 1 like:

...
while (reader.Read())
{
    isAdministrator = reader[0].ToString();
    isAdmin = Convert.ToBoolean(isAdministrator);
    ViewBag.IsAdmin = isAdmin;
    break;
}   
...

and then in the _Layout.cshtml you can use variable from ViewBag like:

@if(ViewBag.IsAdmin == 1)
{
    <div>
    </div>
}

But In my opinion you should look for the SimpleMembership functionality.

Then you can use:

@if(HttpContext.Current.User.IsInRole("Administrator"))
{
    <div>
    </div>
}