[英]Pass current user in ASP.NET MVC controller to DAL
I am using windows authentication for my ASP.NET application. 我正在为我的ASP.NET应用程序使用Windows身份验证。 Fetching the current user name is fairly straightforward in razor views ie
@User.Identity.Name
在剃刀视图中,即
@User.Identity.Name
获取当前用户名非常简单。
The feature I am implementing is that I pass the current user name in my sql select query and get the associated role from the table. 我要实现的功能是在sql select查询中传递当前用户名,并从表中获取关联的角色。 Next, I would like to pass the role back to the controller so that I can access it in all of my application views.
接下来,我想将角色传递回控制器,以便可以在所有应用程序视图中访问它。
So that I could perform operations in my views like 这样我就可以在视图中执行操作
@if (CurrentUserRole = "Admin") {
// Do something
} else {
// So something else
}
HomeController.cs HomeController.cs
using System;
using System.Web;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using nmvs_db.dal;
using nmvs_module;
using nmvs_module.util;
using SecureMedi.Models;
using SecureMedi.DAL;
namespace SecureMedi.Controllers {
public class HomeController: Controller {
static HomeController() {
foreach(string s in new string[] {
"com.ibm.oauth.OAuthUtils",
"nmvs_server",
"nmvs_module"
}) {
var l = org.apache.log4j.Logger.getLogger(s);
l.addAppender(new DotNetAppender());
l.debug(s + " test");
}
}
public IActionResult Error() {
return View();
}
public IActionResult Index() {
ViewData["TextAreaResult"] = "No result yet";
return View();
}
public IActionResult AnotherPage() {
ViewData["TextAreaResult"] = "No result yet";
return View();
}
}
}
UsersDAL.cs (DAL) UsersDAL.cs(DAL)
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using SecureMedi.Models;
namespace SecureMedi.DAL {
public class UsersDAL {
public void CurrentUser(string currentUserName) {
string connectionstring = "MY_CONNECTION_STRING";
string sql = String.Format("select users.name 'Username', roles.name 'Role' from sys.database_principals users inner join sys.database_role_members memberof on users.principal_id = memberof.member_principal_id inner join sys.database_principals roles ON memberof.role_principal_id = roles.principal_id and roles.type = 'R' where users.name = '[{0}]'", currentUserName);
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read()) {
string CurrentUserRole = rdr["Role"].ToString();
rdr.Close();
}
conn.Close();
}
}
}
Running a query against DB table directly returns something like 对数据库表运行查询直接返回类似
Query 询问
select users.name 'Username', roles.name 'Role' from sys.database_principals users inner join sys.database_role_members memberof on users.principal_id = memberof.member_principal_id inner join sys.database_principals roles ON memberof.role_principal_id = roles.principal_id and roles.type = 'R' where users.name = 'Domain\username'
Result 结果
Username Role
Domain\username Admin
Also, I am not 100% sure if I am passing the currentUserName
arg correctly in my CurrentUser
function. 此外,我不确定我是否在
CurrentUser
函数中正确传递了currentUserName
arg。
I have looked up on How to get the current user in ASP.NET MVC and How to pass a value from ASP.NET MVC controller to ASP.NET webforms control inside MVC View? 我查找了如何在ASP.NET MVC中获取当前用户以及如何将值从ASP.NET MVC控制器传递到MVC视图内的ASP.NET Webforms控件? but I am not sure how to pass the value of current user name to and fro from my controller and DAL.
但是我不确定如何在控制器和DAL之间来回传递当前用户名的值。
public CurrentUser GetCurrentUser(string currentUserName) {
string connectionstring = "MY_CONNECTION_STRING";
string sql = String.Format("select users.name 'Username', roles.name 'Role' from sys.database_principals users inner join sys.database_role_members memberof on users.principal_id = memberof.member_principal_id inner join sys.database_principals roles ON memberof.role_principal_id = roles.principal_id and roles.type = 'R' where users.name = '[{0}]'", currentUserName);
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
CurrentUser currentUser = new CurrentUser();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read()) {
currentUser.Role = rdr["Role"].ToString();
currentUser.Username = currentUserName;
rdr.Close();
}
conn.Close();
return currentUser;
}
public class CurrentUser
{
public string Username {get;set;}
public string Role {get;set;}
}
in controller : 在控制器中:
public IActionResult Index() {
UsersDal dal = new UsersDal();
ViewData["CurrentUser"] = dal.GetCurrentUser("username");
return View();
}
in cshtml: 在cshtml中:
@{
var user = ViewData["CurrentUser"];
}
<b>@user.Username</b>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.