相关疑难解决方法(0)

用户处于"admin"角色,但[Authorize(Roles ="admin")]将不会进行身份验证

我在SO上找到了一个很好的答案,描述了如何设置自定义用户角色,我在我的项目中也做了同样的事情.所以在我的登录服务中,我有:

public ActionResult Login() {
  // password authentication stuff omitted here
  var roles = GetRoles(user.Type); // returns a string e.g. "admin,user"
  var authTicket = new FormsAuthenticationTicket(
                    1,
                    userName,
                    DateTime.Now,
                    DateTime.Now.AddMinutes(20), // expiry
                    false,
                    roles,
                    "/");
  var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, 
    FormsAuthentication.Encrypt(authTicket));
  Response.Cookies.Add(cookie);
  return new XmlResult(xmlDoc); // don't worry so much about this - returns XML as ActionResult
}
Run Code Online (Sandbox Code Playgroud)

在Global.asax.cs中,我(从另一个答案中逐字复制):

protected void Application_AuthenticateRequest(Object sender, EventArgs e) {
  var authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
  if (authCookie != null) {
    var authTicket = …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-mvc

10
推荐指数
1
解决办法
1万
查看次数

授权不使用角色的属性

我在使用Authorize属性来处理角色时遇到了麻烦.这就是我装饰我的控制器的方式:

[Authorize(Roles = "admin")]
public ActionResult Index()
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

这就是我记录用户的方式:

string roles = "admin";
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
    1,
    username,
    DateTime.Now,
    DateTime.Now.AddMinutes(30),
    false,
    roles
);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
HttpContext.Current.Response.Cookies.Add(cookie);
Run Code Online (Sandbox Code Playgroud)

但我的用户仍被拒绝访问.我哪里错了?

c# asp.net asp.net-mvc forms-authentication

4
推荐指数
1
解决办法
1万
查看次数

我可以在不使用会员资格的情况下使用user.IsInRole吗?

我正在使用Forms身份验证,我想利用角色,我可以以某种方式设置没有成员资格的用户角色吗?

asp.net asp.net-mvc

3
推荐指数
1
解决办法
7128
查看次数

如何在ASP.NET MVC中为HttpContext.User赋值?

我写了一个如下控制器:

public class AccountController : Controller
{
    public ActionResult Login(/*---*/)
    {
        GenericIdentity identity = new GenericIdentity("userName");
        GenericPrincipal principal = new GenericPrincipal(identity, new string[] { "role1", "role2" });
        this.HttpContext.User = principal;
        /*---*/;
    }
}
Run Code Online (Sandbox Code Playgroud)

登录后,我可以通过其他控制器中的User.Identity.Name获取用户名.但User.IsInRole("role1")始终返回false.

如何为User分配值,我不想使用Membership ...

asp.net-mvc authorize genericprincipal

3
推荐指数
1
解决办法
3127
查看次数

ASP.NET MVC4 - 限制未登录用户的所有页面

我正在使用ASP.NET MVC 4来构建一个简单的Intranet Web应用程序.我已经建立了登录系统并配置了RouteConfig.cs以将登录视图显示为"主页".

我的问题很简单:我还有其他观点,如果他们没有登录,"访客"就无法访问.为了执行测试,我尝试直接通过URL访问这些页面并且它可以工作.如何通过阻止访问所有页面(登录页面除外)来确保这一点?

我已经阅读了有关Web.config和授权的内容,但没有任何效果.有帮助吗?

编辑:正如Khanh TO所说,使用AutorizeAttribute是最好的方法.但是,我还有一点问题.当我以简单用户身份登录时,我应该无法看到管理页面(就是这种情况)但是当我尝试这样做时,它会将我重定向到登录页面(我已经登录了!) .我认为这是因为下面的代码.问题是,如果用户试图尝试未经授权的页面,我想重定向到主页.可以这样做吗?

这是我所说的代码:

<authentication mode="Forms">
      <forms loginUrl="~/User/Login" timeout="2880" />
</authentication>
Run Code Online (Sandbox Code Playgroud)

此外,我想要精确地将我的用户存储到一个简单的表中,其中角色由bool类型定义(isAdmin以了解用户是否是管理员).

c# security authentication asp.net-mvc

2
推荐指数
1
解决办法
6070
查看次数