ASP.NET MVC窗体身份验证+授权+属性简单角色 [英] ASP.NET MVC Forms Authentication + Authorize Attribute + Simple Roles

查看:295
本文介绍了ASP.NET MVC窗体身份验证+授权+属性简单角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想补充的简单的身份验证和授权的ASP.NET MVC应用程序。

I'm trying to add simple Authentication and Authorization to an ASP.NET MVC application.

我只是想对一些附加的功能钉基本窗体身份验证(由于简单性和自定义的数据库结构)

I'm just trying to tack on some added functionality to the basic Forms Authentication (due to simplicity and custom database structure)

假设这是我的数据库结构:
用户:
  用户名
  密码
  角色(理想情况下一些枚举。字符串如果需要的话,目前用户只有一个角色,但是这可能会改变)

Assuming this is my database structure: User: username password role (ideally some enum. Strings if need be. Currently, user only has ONE role, but this might change)

高级问题:
鉴于上述数据库结构,我想能够做到以下几点:

High Level Problem: Given the above database structure, I would like to be able to do the following:


  • 使用简单登录表单验证

  • 装饰我的行动有:
    [授权(角色= {MyRoles.Admin,MyRoles.Member})]

  • 使用我的意见的角色(确定链接在一些谐音显示)

目前,所有我真的肯定的是如何进行身份验证。之后,我迷路了。我不知道在这一点上我抢用户角色(登录,每个授权?)。由于我的角色可能不字符串,我不知道他们会如何适应与User.IsInRole()。

Currently, all I'm really sure of is how to Authenticate. After that I'm lost. I'm not sure at which point do I grab the user role (login, every authorization?). Since my roles may not be strings, I'm not sure how they will fit in with the User.IsInRole().

现在,我问这里是因为我还没有找到一个简单的完成我需要什么。我已经看到多个例子。

Now, I'm asking here because I haven't found a "simple" accomplish what I need. I have seen multiple examples.

有关身份验证:


  • 我们有检查数据库和SetAuthCookie
  • 简单的用户验证
  • 或者我们覆盖了会员供应商,这样做的ValidateUser内
    在这两种,我不知道如何钉在我的简单的用户角色,使他们与工作:
    HttpContext.Current.User.IsInRole(管理员)
    此外,我不知道如何修改这与我的枚举值工作。

  • We have simple user validation that checks the database and "SetAuthCookie"
  • Or we override the Membership provider and do this inside of ValidateUser In either of these, I'm not sure how to tack on my simple user Roles, so that they work with the: HttpContext.Current.User.IsInRole("Administrator") Furthermore, I'm not sure how to modify this to work with my enum values.

有关授权,我已经看到了:

For Authorization, I've seen:


  • 派生AuthorizeAttribute和实施AuthorizeCore或OnAuthorization来处理角色?

  • 实施的IPrincipal?

任何援助将大大AP preciated。不过,我担心我可能需要很多的细节,因为没有什么我GOOGLE似乎适合什么我需要做的。

Any assistance would be greatly appreciated. However, I fear I may need a lot of detail, because none of what I've Googled seems to fit with what I need to do.

推荐答案

建立一个自定义的 AuthorizeAttribute ,可以使用您的枚举,而不是字符串。当你需要授权,通过附加枚举类型名称+枚举值转换成枚举字符串和使用 IsInRole 从那里。

Build a custom AuthorizeAttribute that can use your enums rather than strings. When you need to authorise, convert the enums into strings by appending the enum type name + the enum value and use the IsInRole from there.

要角色添加到您需要连接到的HttpApplication 的AuthenticateRequest 事件像第一$授权用户C $℃的 http://www.eggheadcafe.com/articles/20020906.asp (但反转大量嵌套的if语句到保护条款!)。

To add roles into an authorised user you need to attach to the HttpApplication AuthenticateRequest event something like the first code in http://www.eggheadcafe.com/articles/20020906.asp ( but invert the massively nested if statements into guard clauses!).

您可以往返用户角色的窗体身份验证Cookie或从数据库中每一次抓住他们。

You can round-trip the users roles in the forms auth cookie or grab them from the database each time.

这篇关于ASP.NET MVC窗体身份验证+授权+属性简单角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆