c# - Passing ADFS authenticated user's UPN to SQL string for connection
问题描述
I'm trying to pull information from the MS SQL database based on the UPN of the logged in user. I guess it would seem somewhat trivial for some.
In controller I get user's UPN:
var identity = (ClaimsIdentity)User.Identity;
loggedInUser.UserHandle = identity.Claims.Where(c => c.Type == ClaimTypes.Upn).First().Value;
//get userHandle from UPN
int index = loggedInUser.UserHandle.LastIndexOf("@");
if (index > 0)
loggedInUser.UserHandle = loggedInUser.UserHandle.Substring(0, index);
Below this I try to get the data:
String sql = "Select * from [MyDatabase].[dbo].[TableName] where UserHandle = @userHandle";
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add(new SqlParameter("userHandle", loggedInUser.UserHandle));
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
user.FirstName = reader["FirstName"].ToString();
user.LastName = reader["LastName"].ToString();
}
conn.Close();
}
return View(loggedInUser);
I can see the correct UserHandle in View using @Model.UserHandle. But it returns null in Controller so I can't get the firstName and LastName from the table.
What am I doing wrong? How can I get the value of UPN to be used in my sql string so I can get data from the table?
解决方案
将其留在这里以防万一。
我需要传递“用户”而不是“登录用户”:
return View(user);
推荐阅读
- python - 如何从 matplotlib 条形图中获取数据
- c# - C# - 获取数组最后一个非零数字的索引
- php - 如何在 LEMP 堆栈上启用管理员皮肤?
- datadog - 如何过滤 DataDog 代理中的 statsd 指标?
- actions-on-google - 改变槽位进行处理
- node.js - 如何通过 React 显示通过 Express.js 保存在 MongoDB 中的图像?
- c# - 如何根据您在checkedListBox中签入的项目填写listBox?(使用 Access 数据库)
- ios - 带有内部开关盒的单元测试 API 调用
- firefox - 如何在 Firefox 中禁用 websockets 支持?
- c# - C# Entity Framework Core 使用本机枚举数据类型存储枚举