使用和ArrayList显示从MVC控制器到剃须刀页面的数据
我对使用数组列表的工作非常新鲜,并且想学习如何从MVC Razor页面中具有数组的控制器显示数据。我正在创建一个正常工作的登录名,但想显示从Active Directory到userLogIndata.cshtml页面的捕获的数据。我读到最好的方法是通过模型。任何帮助将不胜感激! 我只想在剃须刀数据页面上的表中的阵列
(
public ActionResult UserLoginData(string username, string LblUserName, string
UserName, ArrayList AuthorizationGroups)
{
UserLoginModel model = new UserLoginModel();
UserName = username;
ViewBag.UserName = UserName;
model.UserName = UserName;
model.LblUserName = model.UserName;
model.AuthorizationGroups = AuthorizationGroups;
foreach (var item in AuthorizationGroups)
{
// Console.WriteLine(item);
model.item = item;
}
return View(model);
}
。
//Labels for UserLoginData:
public string LblUserName { get; set; }
public string LblTitle { get; set; }
public string LblPlantLocation { get; set; }
public string LblUserLoggedInTimeStamp { get; set; }
public string LblUserLoggedOutTimeStamp { get; set; }
public string LblDisplayName { get; set; }
public string LblEmail { get; set; }
//For the ArrayList of MemberGroups.
public ArrayList AuthorizationGroups { get; set; }
public ArrayList YardDogUserGroupMembers { get; set; }
public ArrayList MemberOfUserGroups { get; set; }
public ArrayList YardDogAdminGroupMembers { get; set; }
public object item { get; set; }
中
@model PW_Login.Models.UserLoginModel
@{
Layout = null;
/*
WebGrid webGrid = new WebGrid(source: Model, canPage: true, canSort: true,
sortDirectionFieldName: "PlantLocation", rowsPerPage: 50);
webGrid.Pager(WebGridPagerModes.All);
*/
}
<!DOCTYPE html>
<html>
<head>
<link href="~/Content/UserLogin.css" rel="stylesheet" />
<meta name="viewport" content="width=device-width" />
</head>
<body>
@using (Html.BeginForm("UserLoginData", "LoginController", FormMethod.Post, new { id =
"LoginDataForm", Class = "LoginDataForm" }))
{
//Html.ListBoxFor(model=>model.AuthorizationGroups, Model.AuthorizationGroups)
// string UserName = Session["UserName"].ToString();
<label></label>
//Html.LabelFor(model => model.UserName, @Model.UserName)
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.UserPlantLocation)
</th>
</tr>
<tr>
<td>
<!--- Html.DisplayFor(modelItem => item.AuthorizationGroups) -->
@foreach ( var item in Model.AuthorizationGroups)
{
@Html.DisplayNameFor(Modelitem=>item)
}
</td>
</tr>
</table>
}
</body>
</html>
列表
private void ShowUserInformation(SearchResult rs, string UserName)
{
UserLoginModel model = new UserLoginModel();
Cursor.Current = Cursors.Default;
model.UserName = UserName;
Session["UserName"] = UserName;
Session["LblUserName"] = UserName;
DateTime now = DateTime.Now;
string UserLoggedInTimeStamp = now.ToString();
model.LblUserLoggedInTimeStamp = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss tt");
//Push the UserName into the Label via the model.
if (rs.GetDirectoryEntry().Properties["samaccountname"].Value != null)
model.LblUserName = "Username : " +
rs.GetDirectoryEntry().Properties["samaccountname"].Value.ToString();
if (rs.GetDirectoryEntry().Properties["title"].Value != null)
model.LblTitle = "Title : " +
rs.GetDirectoryEntry().Properties["title"].Value.ToString();
//description returns null... need to find out active directory folder/subfolder.
if (rs.GetDirectoryEntry().Properties["physicaldeliveryofficename"].Value != null)
//PhysicalDeliveryOfficeName returns 110.
model.LblPlantLocation = "PlantLocation : " +
rs.GetDirectoryEntry().Properties["physicaldeliveryofficename"].Value.ToString();
if (rs.GetDirectoryEntry().Properties["member"].Value != null)
model.LblMemberGroup = "Member : " +
rs.GetDirectoryEntry().Properties["member"].Value.ToString();
//DisplayName or DistinguishedName is what I do believe is in the security group
"YardDogUser" or "YardDogAdmin".
if (rs.GetDirectoryEntry().Properties["distinguishedName"].Value != null)
model.LblDistinguishedName = "distinguishedName : " +
rs.GetDirectoryEntry().Properties["distinguishedName"].Value.ToString();
//NULL...
if (rs.GetDirectoryEntry().Properties["YardDogAdmin"].Value != null)
model.LblYardDogAdmin = "YardDogAdmin : " +
rs.GetDirectoryEntry().Properties["YardDogAdmin"].Value.ToString();
if (rs.GetDirectoryEntry().Properties["displayname"].Value != null)
model.LblDisplayName = "Display Name : " +
rs.GetDirectoryEntry().Properties["displayname"].Value.ToString();
if (rs.GetDirectoryEntry().Properties["email"].Value != null)
model.lblEmail = "Email Address : " +
rs.GetDirectoryEntry().Properties["email"].Value.ToString();
/*
//Member Of Office 365 Groups. Use if needed!
///ArrayList MemberOfGroups = new ArrayList();
var MemberOfGroups = new ArrayList(); //perfered way of writing.
string Ret1 = string.Empty;
foreach (object memberOf in rs.GetDirectoryEntry().Properties["memberOf"])
{
MemberOfGroups.Add(Ret1 += " Member Of : " + memberOf.ToString() + "\n");
}
*/
//Get Security Groups that User belongs to. Note: Doesn't show other groups (won't
show YardDogAdmin).
ArrayList SecurityGroups = new ArrayList();
foreach (IdentityReference group in
System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)
{
SecurityGroups.Add(group.Translate(typeof(NTAccount)).ToString());
}
//model doesn't show the correct datetime on these.
model.LblUserLoggedInTimeStamp = model.UserLoggedInTimeStamp.ToString();
model.LblUserLoggedOutTimeStamp = model.UserLoggedOutTimeStamp.ToString();
/******************************************************************************/
//Search to see if this group exists that starts with "YardDog".
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
ArrayList FoundYardDogAdmin = new ArrayList();
ArrayList SecurityGroupsFound = new ArrayList();
// define a "query-by-example" principal - here, we search for a GroupPrincipal
// and with the name like some pattern
GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
qbeGroup.Name = "YardDog*"; //Find all the User Groups for this User that is
logging in.
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
string Ret4 = string.Empty;
// find all matches
foreach (var found in srch.FindAll())
{
FoundYardDogAdmin.Add(Ret4 += " GroupFound : " + found.ToString() + "\n");
SecurityGroupsFound.Add(Ret4 += " GroupFound : " + qbeGroup.ToString() +
"\n");
}
//Count where the User's Display Name exists is needed next. We could do this here
or when we get all the Groups.
}
//Search for all User's of YardDogAdmin Group and list them in the ArrayList.
/*
using PrincipalContext ctxDomain = new PrincipalContext(ContextType.Domain);
{
// get the group you're interested in
GroupPrincipal GroupMembers = GroupPrincipal.FindByIdentity("YardDogAdmin");
ArrayList GroupMembersArray = new ArrayList();
// iterate over its members
foreach (Principal principal in GroupMembers.Members)
{
GroupMembersArray.Add(principal);
}
}
*/
/* Below works, finds all in YardDogAdmin's, YardDogUser's (Finds security groups by
string search). Use if needed.*/
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
//Groups to validate against for current User.
var GroupYardDogAdminPrincipalName = "YardDogAdmin";
var GroupYardDogUserPrincipalName = "YardDogUser";
//Find the current User's Groups.
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, UserName);
//Find all User's within these Groups.
GroupPrincipal YardDogAdminMembers = GroupPrincipal.FindByIdentity(ctx,
GroupYardDogAdminPrincipalName);
GroupPrincipal YardDogUserMembers = GroupPrincipal.FindByIdentity(ctx,
GroupYardDogUserPrincipalName);
//UserGroups that the User logged in belongs to.
if (user != null)
{
var MemberOfUserGroups = new ArrayList();
model.MemberOfUserGroups = MemberOfUserGroups;
var groups = user.GetAuthorizationGroups();
foreach (GroupPrincipal group in groups)
{
MemberOfUserGroups.Add(group);
}
if (MemberOfUserGroups.Contains("YardDogAdmin"))
{
//Pass to the model YardDogAdmin exists for this user (AdminFlag translates to
LocationData table).
model.LblYardDogAdmin = "Y";
model.AdminFlag = "Y";
}
else
{
model.LblYardDogAdmin = "N";
model.AdminFlag = "N";
}
//Get the Members of YardDogAdmin and their warehouse locations.
if (YardDogAdminMembers != null)
{
var YardDogAdminGroupMembers = new ArrayList();
model.YardDogAdminGroupMembers = YardDogAdminGroupMembers;
foreach (Principal principal in YardDogAdminMembers.Members)
{
rs = SearchUserByDisplayName(principal.DisplayName.ToString());
YardDogAdminGroupMembers.Add(principal.DisplayName + " " +
rs.GetDirectoryEntry().Properties["physicaldeliveryofficename"].Value.ToString() + "
YardDogAdmin");
}
}
//Get the Members of YardDogUser and their location.
if (YardDogUserMembers != null)
{
var YardDogUserGroupMembers = new ArrayList();
model.YardDogUserGroupMembers = YardDogUserGroupMembers;
foreach (Principal principal in YardDogUserMembers.Members)
{
rs = SearchUserByDisplayName(principal.DisplayName.ToString());
YardDogUserGroupMembers.Add(principal.DisplayName + " " +
rs.GetDirectoryEntry().Properties["physicaldeliveryofficename"].Value.ToString() + "
YardDogUser");
}
}
}
}
I'm pretty new to working with Array Lists and would like to learn how to display data from the Controller that has an Array in the method to the MVC Razor Page. I'm creating a login that works just fine, but would like to display captured data from Active Directory to the UserLoginData.CSHTML page. I read that the best way is through the model. Any help would be greatly appreciated! (I just want the ArrayList in a table on the Razor data Page.)
Controller:
public ActionResult UserLoginData(string username, string LblUserName, string
UserName, ArrayList AuthorizationGroups)
{
UserLoginModel model = new UserLoginModel();
UserName = username;
ViewBag.UserName = UserName;
model.UserName = UserName;
model.LblUserName = model.UserName;
model.AuthorizationGroups = AuthorizationGroups;
foreach (var item in AuthorizationGroups)
{
// Console.WriteLine(item);
model.item = item;
}
return View(model);
}
Model (Properties):
//Labels for UserLoginData:
public string LblUserName { get; set; }
public string LblTitle { get; set; }
public string LblPlantLocation { get; set; }
public string LblUserLoggedInTimeStamp { get; set; }
public string LblUserLoggedOutTimeStamp { get; set; }
public string LblDisplayName { get; set; }
public string LblEmail { get; set; }
//For the ArrayList of MemberGroups.
public ArrayList AuthorizationGroups { get; set; }
public ArrayList YardDogUserGroupMembers { get; set; }
public ArrayList MemberOfUserGroups { get; set; }
public ArrayList YardDogAdminGroupMembers { get; set; }
public object item { get; set; }
Razor CSHTML:
@model PW_Login.Models.UserLoginModel
@{
Layout = null;
/*
WebGrid webGrid = new WebGrid(source: Model, canPage: true, canSort: true,
sortDirectionFieldName: "PlantLocation", rowsPerPage: 50);
webGrid.Pager(WebGridPagerModes.All);
*/
}
<!DOCTYPE html>
<html>
<head>
<link href="~/Content/UserLogin.css" rel="stylesheet" />
<meta name="viewport" content="width=device-width" />
</head>
<body>
@using (Html.BeginForm("UserLoginData", "LoginController", FormMethod.Post, new { id =
"LoginDataForm", Class = "LoginDataForm" }))
{
//Html.ListBoxFor(model=>model.AuthorizationGroups, Model.AuthorizationGroups)
// string UserName = Session["UserName"].ToString();
<label></label>
//Html.LabelFor(model => model.UserName, @Model.UserName)
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.UserPlantLocation)
</th>
</tr>
<tr>
<td>
<!--- Html.DisplayFor(modelItem => item.AuthorizationGroups) -->
@foreach ( var item in Model.AuthorizationGroups)
{
@Html.DisplayNameFor(Modelitem=>item)
}
</td>
</tr>
</table>
}
</body>
</html>
Method that gets the info from AD in the Controller:
private void ShowUserInformation(SearchResult rs, string UserName)
{
UserLoginModel model = new UserLoginModel();
Cursor.Current = Cursors.Default;
model.UserName = UserName;
Session["UserName"] = UserName;
Session["LblUserName"] = UserName;
DateTime now = DateTime.Now;
string UserLoggedInTimeStamp = now.ToString();
model.LblUserLoggedInTimeStamp = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss tt");
//Push the UserName into the Label via the model.
if (rs.GetDirectoryEntry().Properties["samaccountname"].Value != null)
model.LblUserName = "Username : " +
rs.GetDirectoryEntry().Properties["samaccountname"].Value.ToString();
if (rs.GetDirectoryEntry().Properties["title"].Value != null)
model.LblTitle = "Title : " +
rs.GetDirectoryEntry().Properties["title"].Value.ToString();
//description returns null... need to find out active directory folder/subfolder.
if (rs.GetDirectoryEntry().Properties["physicaldeliveryofficename"].Value != null)
//PhysicalDeliveryOfficeName returns 110.
model.LblPlantLocation = "PlantLocation : " +
rs.GetDirectoryEntry().Properties["physicaldeliveryofficename"].Value.ToString();
if (rs.GetDirectoryEntry().Properties["member"].Value != null)
model.LblMemberGroup = "Member : " +
rs.GetDirectoryEntry().Properties["member"].Value.ToString();
//DisplayName or DistinguishedName is what I do believe is in the security group
"YardDogUser" or "YardDogAdmin".
if (rs.GetDirectoryEntry().Properties["distinguishedName"].Value != null)
model.LblDistinguishedName = "distinguishedName : " +
rs.GetDirectoryEntry().Properties["distinguishedName"].Value.ToString();
//NULL...
if (rs.GetDirectoryEntry().Properties["YardDogAdmin"].Value != null)
model.LblYardDogAdmin = "YardDogAdmin : " +
rs.GetDirectoryEntry().Properties["YardDogAdmin"].Value.ToString();
if (rs.GetDirectoryEntry().Properties["displayname"].Value != null)
model.LblDisplayName = "Display Name : " +
rs.GetDirectoryEntry().Properties["displayname"].Value.ToString();
if (rs.GetDirectoryEntry().Properties["email"].Value != null)
model.lblEmail = "Email Address : " +
rs.GetDirectoryEntry().Properties["email"].Value.ToString();
/*
//Member Of Office 365 Groups. Use if needed!
///ArrayList MemberOfGroups = new ArrayList();
var MemberOfGroups = new ArrayList(); //perfered way of writing.
string Ret1 = string.Empty;
foreach (object memberOf in rs.GetDirectoryEntry().Properties["memberOf"])
{
MemberOfGroups.Add(Ret1 += " Member Of : " + memberOf.ToString() + "\n");
}
*/
//Get Security Groups that User belongs to. Note: Doesn't show other groups (won't
show YardDogAdmin).
ArrayList SecurityGroups = new ArrayList();
foreach (IdentityReference group in
System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)
{
SecurityGroups.Add(group.Translate(typeof(NTAccount)).ToString());
}
//model doesn't show the correct datetime on these.
model.LblUserLoggedInTimeStamp = model.UserLoggedInTimeStamp.ToString();
model.LblUserLoggedOutTimeStamp = model.UserLoggedOutTimeStamp.ToString();
/******************************************************************************/
//Search to see if this group exists that starts with "YardDog".
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
ArrayList FoundYardDogAdmin = new ArrayList();
ArrayList SecurityGroupsFound = new ArrayList();
// define a "query-by-example" principal - here, we search for a GroupPrincipal
// and with the name like some pattern
GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
qbeGroup.Name = "YardDog*"; //Find all the User Groups for this User that is
logging in.
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
string Ret4 = string.Empty;
// find all matches
foreach (var found in srch.FindAll())
{
FoundYardDogAdmin.Add(Ret4 += " GroupFound : " + found.ToString() + "\n");
SecurityGroupsFound.Add(Ret4 += " GroupFound : " + qbeGroup.ToString() +
"\n");
}
//Count where the User's Display Name exists is needed next. We could do this here
or when we get all the Groups.
}
//Search for all User's of YardDogAdmin Group and list them in the ArrayList.
/*
using PrincipalContext ctxDomain = new PrincipalContext(ContextType.Domain);
{
// get the group you're interested in
GroupPrincipal GroupMembers = GroupPrincipal.FindByIdentity("YardDogAdmin");
ArrayList GroupMembersArray = new ArrayList();
// iterate over its members
foreach (Principal principal in GroupMembers.Members)
{
GroupMembersArray.Add(principal);
}
}
*/
/* Below works, finds all in YardDogAdmin's, YardDogUser's (Finds security groups by
string search). Use if needed.*/
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
//Groups to validate against for current User.
var GroupYardDogAdminPrincipalName = "YardDogAdmin";
var GroupYardDogUserPrincipalName = "YardDogUser";
//Find the current User's Groups.
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, UserName);
//Find all User's within these Groups.
GroupPrincipal YardDogAdminMembers = GroupPrincipal.FindByIdentity(ctx,
GroupYardDogAdminPrincipalName);
GroupPrincipal YardDogUserMembers = GroupPrincipal.FindByIdentity(ctx,
GroupYardDogUserPrincipalName);
//UserGroups that the User logged in belongs to.
if (user != null)
{
var MemberOfUserGroups = new ArrayList();
model.MemberOfUserGroups = MemberOfUserGroups;
var groups = user.GetAuthorizationGroups();
foreach (GroupPrincipal group in groups)
{
MemberOfUserGroups.Add(group);
}
if (MemberOfUserGroups.Contains("YardDogAdmin"))
{
//Pass to the model YardDogAdmin exists for this user (AdminFlag translates to
LocationData table).
model.LblYardDogAdmin = "Y";
model.AdminFlag = "Y";
}
else
{
model.LblYardDogAdmin = "N";
model.AdminFlag = "N";
}
//Get the Members of YardDogAdmin and their warehouse locations.
if (YardDogAdminMembers != null)
{
var YardDogAdminGroupMembers = new ArrayList();
model.YardDogAdminGroupMembers = YardDogAdminGroupMembers;
foreach (Principal principal in YardDogAdminMembers.Members)
{
rs = SearchUserByDisplayName(principal.DisplayName.ToString());
YardDogAdminGroupMembers.Add(principal.DisplayName + " " +
rs.GetDirectoryEntry().Properties["physicaldeliveryofficename"].Value.ToString() + "
YardDogAdmin");
}
}
//Get the Members of YardDogUser and their location.
if (YardDogUserMembers != null)
{
var YardDogUserGroupMembers = new ArrayList();
model.YardDogUserGroupMembers = YardDogUserGroupMembers;
foreach (Principal principal in YardDogUserMembers.Members)
{
rs = SearchUserByDisplayName(principal.DisplayName.ToString());
YardDogUserGroupMembers.Add(principal.DisplayName + " " +
rs.GetDirectoryEntry().Properties["physicaldeliveryofficename"].Value.ToString() + "
YardDogUser");
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在控制器中创建一个从一个控制器操作结果到另一个控制器,然后进入模型中的tempdata []。之后,您可以访问tempdata并根据需要将其保留。然后在登录数据页面上,循环从tempdata或模型属性中循环以在标签中显示。
Just create TempData[] in the controller from one controller action result to another and then into the model also. After that you can access the TempData and keep it as needed for that session. Then on the Login Data Page, loop through the objects from the TempData or model properties to display in labels.