我正在使用C#MVC内置授权。我向其中添加了自定义字段“ GroupId”。现在,我试图传递已登录用户的“ GroupId”以显示特定于用户的数据。 我已对登录后显示的页面进行了控制器授权。 我能够将新的Regestation GroupId保存到数据库,但无法获得登录用户的价值。 我在视图中使用User.Identity.GetGroupId(),但出现错误。
帐户视图模型:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace WebApplication1.Models
{
public class ExternalLoginConfirmationViewModel
{
[Required]
[Display(Name = "Email")]
public string Email { get; set; }
[Display(Name = "GroupId")]
public int GroupId { get; set; }
}
public class ExternalLoginListViewModel
{
public string ReturnUrl { get; set; }
}
public class SendCodeViewModel
{
public string SelectedProvider { get; set; }
public ICollection<System.Web.Mvc.SelectListItem> Providers { get; set; }
public string ReturnUrl { get; set; }
public bool RememberMe { get; set; }
}
public class VerifyCodeViewModel
{
[Required]
public string Provider { get; set; }
[Required]
[Display(Name = "Code")]
public string Code { get; set; }
public string ReturnUrl { get; set; }
[Display(Name = "Remember this browser?")]
public bool RememberBrowser { get; set; }
public bool RememberMe { get; set; }
[Display(Name = "GroupId")]
public int GroupId { get; set; }
}
public class ForgotViewModel
{
[Required]
[Display(Name = "Email")]
public string Email { get; set; }
}
public class LoginViewModel
{
[Required]
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
[Display(Name = "GroupId")]
public int GroupId { get; set; }
}
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
[Display(Name = "GroupId")]
public int GroupId { get; set; }
}
public class ResetPasswordViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
public string Code { get; set; }
}
public class ForgotPasswordViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
}}
设备控制器:
[Authorize]
public class DeviceController : Controller
{
[ActionName("Index")]
public async Task<ActionResult> IndexAsync(string sortOrder, string currentFilter, string searchString, int? page)
{
Session["User"] = 18;
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.LastPingSortParm = sortOrder == "LastPing" ? "date_desc" : "LastPing";
ViewBag.AddressSortParm = sortOrder == "Address" ? "date_desc" : "Address";
ViewBag.IPSortParm = sortOrder == "IPaddress" ? "date_desc" : "IPaddress";
if (searchString != null )
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var items = await DocumentDBRepository<Device>.GetDevicemapAsync( d => d.Id != null);
if (!String.IsNullOrEmpty(searchString))
{
items = items.Where(d => d.Name.Contains(searchString));
}
switch (sortOrder)
{
case "name_desc":
items = items.OrderByDescending(d => d.Name);
break;
case "LastPing":
items = items.OrderByDescending(d => d.LastPing);
break;
case "date_desc":
items = items.OrderBy(d => d.LastPing);
break;
case "Address":
items = items.OrderBy(d => d.Address);
break;
case "Address_desc":
items = items.OrderByDescending(d => d.Address);
break;
case "IP":
items = items.OrderBy(d => d.IPaddress);
break;
case "IP_desc":
items = items.OrderByDescending(d => d.IPaddress);
break;
default:
items = items.OrderBy(d => d.Name);
break;
}
int pageSize = 15;
int pageNumber = (page ?? 1);
return View(items.ToPagedList(pageNumber, pageSize));
}
索引视图:
@model PagedList.IPagedList<WebApplication1.Models.Device>
@using PagedList.Mvc;
@using Microsoft.AspNet.Identity
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "Devices"; }
<h2>List Of Devices</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<div class="form">
@using (Html.BeginForm("Index", "Device", FormMethod.Get))
{
<p>
Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
}
</div>
<table class="table">
<tr>
<th>
@Html.ActionLink(" Ip Address", "Index", new { sortOrder = ViewBag.IPSortParm })
</th>
<th width="30%">
@Html.ActionLink("Name", "Index", new { sortOrder = ViewBag.NameSortParm })
</th>
<th>
@Html.ActionLink("Address", "Index", new { sortOrder = ViewBag.AddressSortParm })
</th>
<th>
@Html.ActionLink(" Last Ping", "Index", new { sortOrder = ViewBag.LastPingSortParm })
</th>
<th>
Reports
</th>
<th>
</th>
</tr>
@foreach (var item in Model)
{
if (item.GroupId == User.Identity.GetGroupId())
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.IPaddress)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastPing)
</td>
<td>
<i class="fa fa-car w3-large"></i>
<i class="fas fa-cloud-sun-rain w3-large"></i>
<i class="far fa-folder-open w3-large"></i>
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id, GroupId = item.GroupId })
</td>
</tr>
}
}
</table>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.GroupSortParm }))
我收到以下错误: “ /”应用程序中的服务器错误。 编译错误 说明:编译服务于此请求所需的资源期间发生错误。请查看以下特定的错误详细信息,并适当地修改您的源代码。
编译器错误消息:CS1061:'IIdentity'不包含'GetGroupId'的定义,并且找不到扩展方法'GetGroupId'接受类型为'IIdentity'的第一个参数(您是否缺少using指令或程序集参考?)
源错误:
第49行:@foreach(模型中的变量项)第50行:{第51行:
if(item.GroupId == User.Identity.GetGroupId())第52行:{ 第53行:
源文件:T:\ LED Signs \ Documentation \ Projects \ Azure应用程序开发\ GIT \ Ana29_1_19 \ WebApplication1 \ Views \ Device \ Index.cshtml行:51
显示详细的编译器输出:
显示完整的编译源:
版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET版本:4.7.3282.0