Asp.net mvc asp.net mvc授权问题
我正在尝试向我的控制器添加授权,但它不起作用 我不确定在我的程序中查找到哪里,但添加了Asp.net mvc asp.net mvc授权问题,asp.net-mvc,configuration,controller,authorization,security-roles,Asp.net Mvc,Configuration,Controller,Authorization,Security Roles,我正在尝试向我的控制器添加授权,但它不起作用 我不确定在我的程序中查找到哪里,但添加了 [Authorize] 我的控制器中的过滤器不工作,更不用说像这样的东西了 [Authorize(Roles = "Manager")] 我已经能够在创建新MVC项目时提供的默认应用程序中实现这一点(即,如果我没有登录,我可以将“关于”选项卡重定向到登录屏幕),因此我假设我在构建应用程序的过程中把事情搞砸了。有人知道我应该在哪里解决这个问题吗?我有用户,他们有角色;我使用的是自动创建的ASP.net模式
[Authorize]
我的控制器中的过滤器不工作,更不用说像这样的东西了
[Authorize(Roles = "Manager")]
我已经能够在创建新MVC项目时提供的默认应用程序中实现这一点(即,如果我没有登录,我可以将“关于”选项卡重定向到登录屏幕),因此我假设我在构建应用程序的过程中把事情搞砸了。有人知道我应该在哪里解决这个问题吗?我有用户,他们有角色;我使用的是自动创建的ASP.net模式;我上下检查了我的web.config文件,虽然我对这一点很陌生,但似乎没有什么不合适的地方。我不知道我的授权过滤器为什么不工作。我编写了一个自定义属性来解决这个问题。您可以按如下方式对控制器方法进行属性设置:
[RequiresRole(Role="Admin")]
public ActionResult Index()
{
int i = 5 + 5;
return View();
}
该属性的代码如下所示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace Web.Controllers
{
public class RequiresRoleAttribute : ActionFilterAttribute
{
public string Role { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (string.IsNullOrEmpty(Role))
{
throw new InvalidOperationException("No role specified.");
}
string redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsolutePath;
string redirectUrl = string.Format("?returnUrl={0}", redirectOnSuccess);
string loginUrl = FormsAuthentication.LoginUrl + redirectUrl;
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.HttpContext.Response.Redirect(loginUrl, true);
}
else
{
bool isAuthorised = filterContext.HttpContext.User.IsInRole(this.Role);
if (!isAuthorised)
{
filterContext.HttpContext.Response.Redirect(loginUrl, true);
}
}
}
}
}
我编写了一个自定义属性来解决这个问题。您可以按如下方式对控制器方法进行属性设置:
[RequiresRole(Role="Admin")]
public ActionResult Index()
{
int i = 5 + 5;
return View();
}
该属性的代码如下所示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace Web.Controllers
{
public class RequiresRoleAttribute : ActionFilterAttribute
{
public string Role { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (string.IsNullOrEmpty(Role))
{
throw new InvalidOperationException("No role specified.");
}
string redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsolutePath;
string redirectUrl = string.Format("?returnUrl={0}", redirectOnSuccess);
string loginUrl = FormsAuthentication.LoginUrl + redirectUrl;
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.HttpContext.Response.Redirect(loginUrl, true);
}
else
{
bool isAuthorised = filterContext.HttpContext.User.IsInRole(this.Role);
if (!isAuthorised)
{
filterContext.HttpContext.Response.Redirect(loginUrl, true);
}
}
}
}
}
你能显示你的Web.config吗?至少有成员资格、身份验证、连接字符串和角色管理器部分?您能显示您的Web.config吗?至少是成员资格、身份验证和连接字符串和角色管理器部分?