我在webpages_UsersInRoles中添加了两个用户和两个角色。我正在尝试检查用户登录应用程序的角色。
这是我的代码:
var UserId = WebSecurity.GetUserId(HttpContext.Current.User.Identity.Name);
var User = WebSecurity.CurrentUserName;
var db = Database.Open("ApplicationServices");
var stQueryString = "SELECT * FROM Table1 WHERE (UserId) = (UserId) ORDER BY SubmitDate DESC";
var data = db.Query(stQueryString, UserId);
var grid = new WebGrid(data, rowsPerPage:20);
var AdminQueryString = "SELECT * FROM Table1 ORDER BY SubmitDate DESC";
var AdminData = db.Query(AdminQueryString);
var AdminGrid = new WebGrid(AdminData, rowsPerPage:20);
<div id="Divgrid">
@if (Roles.IsUserInRole(UserId, "admin") && (AdminData.Any())){
@AdminGrid.GetHtml(
tableStyle: "grid",
headerStyle: "grid-header",
footerStyle: "grid-footer",
alternatingRowStyle: "grid-alternating-row",
selectedRowStyle: "grid-selected-row",
rowStyle: "grid-row-style",
columns: AdminGrid.Columns(
AdminGrid.Column(header:"", format:@<a href="View/@item.Id">View</a>),
AdminGrid.Column(header:"", format:@<a href="Treatment/@item.Id">Treatment</a>),
AdminGrid.Column("Name", format:@<text>@item.ClientName</text>),
AdminGrid.Column("Date", format:@<text>@item.SubmitDate</text>))) }
else if (Roles.IsUserInRole(UserId, "user") && (data.Any())) {
@grid.GetHtml(
tableStyle: "grid",
headerStyle: "grid-header",
footerStyle: "grid-footer",
alternatingRowStyle: "grid-alternating-row",
selectedRowStyle: "grid-selected-row",
rowStyle: "grid-row-style",
columns: grid.Columns(
grid.Column(header:"", format:@<a href="View/@item.Id">View</a>),
grid.Column(header:"", format:@<a href="Treatment/@item.Id">Treatment</a>),
grid.Column("Name", format:@<text>@item.ClientName</text>),
grid.Column("Date", format:@<text>@item.SubmitDate</text>))) }
</div>
以下是我遇到的错误:
CS1502:'System.Web.Security.Roles.IsUserInRole(string,string)'的最佳重载方法匹配有一些无效的参数
答案 0 :(得分:0)
IsUserInRole有两个参数,第一个是用户名,而不是用户ID。您可以使用WebSecurity.CurrentUserName代替您具有UserID的位置。
一个问题是查询需要更改为允许将用户名作为参数传递:
var stQueryString = "SELECT * FROM Table1 WHERE UserId = @0 ORDER BY SubmitDate DESC";
@ 0表示在查询中传递参数。