简体   繁体   English

Windows Azure基于角色的身份验证(ACS)

[英]Windows Azure Role Based authentication (ACS)

I have a Asp.net mvc4 project running on Windows Azure Cloud already. 我已经在Windows Azure Cloud上运行了Asp.net mvc4项目。 But so far it hasn't any user management thing. 但是到目前为止,它还没有任何用户管理功能。 any one can log in(I have a windows live single sign on). 任何人都可以登录(我有一个Windows Live单点登录)。 But now I need to do the simplest role based authentication. 但是现在我需要做基于角色的最简单的身份验证。

1.I want to register user roles(administrator,user,content manager)/it can be done manually. 1.我想注册用户角色(管理员,用户,内容管理员)/可以手动完成。 2.I need to authenticate users based on roles when they log in . 2.我需要根据用户登录时的角色对他们进行身份验证。

Can any one give me a hint, or link to a clear tutorial or suggest me any idea? 任何人都可以给我提示,或者链接到清晰的教程,或者给我提出任何想法吗?

The simplest is actually to add "role" claims in the token issued by ACS. 最简单的方法实际上是在ACS发行的令牌中添加“角色”声明。 Since you said you are ok in doing this manually for now, then you'd use the ACS portal to create these rules. 既然您说现在可以手动执行此操作,那么您将使用ACS门户创建这些规则。

In your app, you'd do the usual thing, like decorate the actions with the "Authorize" attribute, including roles: 在您的应用中,您将执行通常的操作,例如使用“ Authorize”属性装饰动作,包括角色:

[Authorize(Roles="Administrator")]
public ActionResult Index()
{
  var b = User.IsInRole("Manager");
...
}

As long as you use "Role" claim types, everything works. 只要您使用“角色”声明类型,一切都会起作用。 (This can also be customized, but it works out of the box this way). (这也可以自定义,但是可以通过这种方式立即使用)。

The only challenge you will have is due to LiveID. 您将面临的唯一挑战是LiveID。 LiveID gives you just a unique identifier. LiveID仅给您一个唯一的标识符。 You need a way of mapping that identifier with a know user (eg e-mail or name). 您需要一种将标识符与已知用户(例如电子邮件或姓名)进行映射的方法。 This usually requires a two step process. 这通常需要两步过程。 You first authenticate and get the unique id, then you ask the user for its information and validate it (by sending an e-mail for example). 您首先进行身份验证并获得唯一的ID,然后要求用户提供其信息并进行验证(例如,通过发送电子邮件)。

Using any of the other identity providers, you don't have this problem, because all of them give you an e-mail and a name. 使用任何其他身份提供程序,您都不会遇到此问题,因为它们都给您提供了电子邮件和名称。

Writing the above mentioned rules is as easy as: 编写上述规则很容易:

  • email: joy@mail.com -> role:administrator 电子邮件:joy@mail.com->角色:管理员
  • email: someone@mail.com -> role: manager ... 电子邮件:someone@mail.com->角色:经理...

If you have a larger number of users or a larger number of rules then the portal is often no longer practical and you will need something else (eg use the API from your app, use scripting, use a tool like auth10 , etc.) 如果您有更多的用户或更多的规则,则门户通常不再实用,并且您将需要其他功能(例如,使用应用程序中的API,使用脚本,使用auth10之类的工具等)。

The way I solved it for my project was to add a ClaimsAuthenticationManager and add the user's roles to the identity there. 我为项目解决该问题的方法是添加ClaimsAuthenticationManager并将用户角色添加到那里的身份中。

namespace Claims
{
  public class RoleClaimsAuthenticationManager : ClaimsAuthenticationManager

    public override ClaimsPrincipal Authenticate(string resourceName, ClaimsPrincipal incomingPrincipal)
    {
        if (incomingPrincipal != null && incomingPrincipal.Identity.IsAuthenticated)
        {
            var identity = (ClaimsIdentity)incomingPrincipal.Identity;
            var roles = getRolesForIdentity(identity); //Get the roles for your identity here
            foreach (var r in roles)
            {
                 identity.AddClaim(new Claim(ClaimTypes.Role, r.Name));
            }
        }

        return base.Authenticate(resourceName, incomingPrincipal);
    }
}

I then hook it up in the config in the system.identityModel -> identityConfiguration section 然后将其连接到system.identityModel -> identityConfiguration部分的配置中

<claimsAuthenticationManager type="Claims.RoleClaimsAuthenticationManager, Claims" />

As you might have noticed ACS takes care of authentication and not authorization, you'll need to handle that yourself. 您可能已经注意到ACS负责身份验证而不是授权,因此您需要自己处理。

The easiest thing to do is to create a 'profile' in your application each time a new user connects. 最简单的方法是每次有新用户连接时在应用程序中创建一个“配置文件”。 When creating the profile for a specific user you'll need to store the identity provider and the name of that user together with the profile. 为特定用户创建配置文件时,您需要将身份提供者和该用户的名称与配置文件一起存储。 Storing this info will allow you to fetch the profile for that user the next time he connects (you'll get this info from the claims, but this depends on how you configured ACS). 存储此信息将使您下次连接该用户时可以获取该用户的配置文件(您将从声明中获取此信息,但这取决于您配置ACS的方式)。

For a fully working example you should take a look at the source of the BlobShare application (which uses authentication and authorization). 对于完整的示例,您应该查看BlobShare应用程序的源代码(使用身份验证和授权)。

An option other than ACS is memebership services. 除ACS之外的一种选择是会员服务。

Introduction to Membership 会员简介

With membership services you have both authentication and authorization. 使用会员服务,您既可以进行身份​​验证也可以进行授权。 Membership has roles and you can even use the roles in the web.config. 成员资格具有角色,您甚至可以在web.config中使用角色。

ACS is not a bad option. ACS并不是一个不错的选择。 I am only presenting another option. 我只提出另一种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM