asp.net-mvc – 如何在asp.net mvc4应用程序中显示注册用户列表
发布时间:2020-05-25 10:40:20 所属栏目:asp.Net 来源:互联网
导读:我使用razor引擎创建了asp.net mvc4应用程序,我对这项技术不熟悉,并尝试找出一种在管理员登录后向管理员显示注册用户列表的方法.成员资格使用system.web.providers.谁能说 – 首先,如何使用实体框架为用户创建单独的角色 其次,如何获取并向管理员显示具有不同
|
我使用razor引擎创建了asp.net mvc4应用程序,我对这项技术不熟悉,并尝试找出一种在管理员登录后向管理员显示注册用户列表的方法.成员资格使用system.web.providers.谁能说 –
提前致谢. 解决方法[Authorize(Roles = "Admin")]
public ActionResult Index()
{
using (var ctx = new UsersContext())
{
return View(ctx.UserProfiles.ToList());
}
}
并在视图中: @using MvcApplication1.Models
@model IEnumerable<UserProfile>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<h2>Users list</h2>
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
</tr>
</thead>
<tbody>
@foreach (var user in Model)
{
<tr>
<td>@user.UserId</td>
<td>@user.UserName</td>
</tr>
}
</tbody>
</table>
</body>
</html>
当然,为了能够访问/ users / index控制器操作,您需要首先拥有用户和角色.只有Admin角色的用户才能调用它. 这是一个 以下是示例迁移配置的外观: internal sealed class Configuration : DbMigrationsConfiguration<UsersContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(UsersContext context)
{
WebSecurity.InitializeDatabaseConnection(
"DefaultConnection","UserProfile","UserId","UserName",autoCreateTables: true
);
if (!Roles.RoleExists("Admin"))
{
Roles.CreateRole("Admin");
}
if (!WebSecurity.UserExists("john"))
{
WebSecurity.CreateUserAndAccount("john","secret");
}
if (!Roles.GetRolesForUser("john").Contains("Admin"))
{
Roles.AddUsersToRoles(new[] { "john" },new[] { "Admin" });
}
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 在部分视图中强制使用没有Html.BeginForm / Aja
- asp.net – 检查该电子邮件地址是否适用于System.Net.Mail.
- asp.net-mvc – 使用没有主键的查找在dbSet中查找记录
- asp.net – 如何从SQL数据库流.flv文件
- asp.net-mvc-3 – 如果Controller.OnAuthorization()返回vo
- ASP.NET sessionState SQLServer模式超时不起作用
- ASP.NET’Session.Remove(key)’v / s’Session(key)= noth
- asp.net – 通过调用.ashx页面下载文件
- asp.net – 无法访问已关闭的文件
- asp.net-mvc – Windows 8 VS2012 IISExpress Windows身份验
推荐文章
站长推荐
- asp.net – ASP计数器 – 不同计数器“桶”中类似
- asp.net-mvc – 在多个表单上指定验证摘要
- asp.net-mvc – 使用Html.BeginForm与querystrin
- file-upload – 增加Kestrel的上传请求长度限制
- asp.net-mvc – 控制台应用程序HttpClient发布到
- asp.net – 在AJAX方法调用中的RegisterClientSc
- asp.net – MSDeploy连接字符串的自动加密,字典中
- asp.net – How2:在HttpModule中挂钩的事件,用于
- asp.net-mvc – FluentValidation客户端验证
- asp.net – MVC 3 System.Web.Optimization捆绑单
热点阅读
