【问题标题】:How to authorize an OpenID user on MVC Controller如何在 MVC 控制器上授权 OpenID 用户
【发布时间】:2010-06-25 02:27:29
【问题描述】:

我正在查看 MVC 中可用的 [Authorize(Roles = "DefaultUser")],但我似乎无法确定是否可以将它与 DotNetOpenAuth 一起使用。

我使用 OpenID 作为我唯一的会员资格提供商,但我在 UserProfile 表中有一些通用项目。我不希望除了适当的用户之外的任何人都能够访问编辑控制器。

示例:
UserID 2 应该能够访问/Users/Edit/1,但他们可以访问/Users/Edit/2

【问题讨论】:

    标签: asp.net-mvc permissions openid membership dotnetopenauth


    【解决方案1】:

    编辑/重写以获得更清晰的理解

    public class AdvancedAuthorizeAttribute : AuthorizeAttribute
        {
                public string RouteDataValue
                {
                    get;
                    set;
                }
    
                protected override bool AuthorizeCore(HttpContextBase httpContext)
                {
                    if(base.AuthorizeCore(httpContext))
                    {
                        MvcHandler mvcHandler = httpContext.CurrentHandler as MvcHandler;
                        if (mvcHandler != null)
                        {
                            var paramValue = mvcHandler.RequestContext.RouteData.Values[RouteDataValue];
                            if (paramValue != null)
                            {
                                // Inside this IF-statement is where you'll have to customize for your code.
    
                                //I use the default user from HttpContext
                                string UserPrincipalName = httpContext.User.Identity.Name;
                                // I use email as login name, and here I try to fetch a user from my repository with that email.
                                User userObject = new Repository<User>().GetOne(x => x.Email == UserPrincipalName);
                                // If I find a user, and that user's UserID is equal to the value in paramValue, it's Ok! Otherwise, not.
                                return (userObject != null && userObject.UserID.ToString() == paramValue.ToString());
                            }
                        }
                    }
                    return false;
                }
        }
    

    然后,使用它:

    // Example usage (for instance from UserController)
    [AdvancedAuthorize(RouteDataValue="userID")]
    public ActionResult Edit(int userID)
    {
         // everything as normal
    }
    
    [AdvancedAuthorize(RouteDataValue="userEmail")]
    public ActionResult Delete(string userEmail)
    {
         // everything as normal
    }
    

    当然,要让它工作,动作示例中的userIDuserEmail需要被modelbinder绑定(参数必须存在于RouteData.Values中)才能工作。

    【讨论】:

    • 这会被放入void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)吗?
    • 没有。您创建扩展 AuthorizeAttribute 并覆盖 AuthorizeCore 的 UserIDAuthorizeAttribute(或等效项)。在那里,你检查 if(base.AuthorizeCore(context)) { ...put code above here ... }
    • 对不起...我真的没有任何线索。
    【解决方案2】:

    我不认为[Authorize(Roles = "DefaultUser")] 意味着人们只能编辑自己的个人资料。而是意味着 DefaultUser 角色中的每个人都可以编辑配置文件......这是关于它的讨论,这几乎是同一个问题:asp.net mvc authorization using roles

    编辑: 好的,那么您的问题是角色和身份不适用于 OpenID?只有当HttpContext.Current.User 包含正确的主体时,Authorize 才有效。如果您使用 OpenID,这不会开箱即用,但您必须在 Global.asax 中执行类似的操作:

    void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
    {
        HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            string encTicket = authCookie.Value;
            if (!String.IsNullOrEmpty(encTicket))
            {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(encTicket);
                YourAppIdentity id = new YourAppIdentity(ticket);
                string username = ticket.Name;
                string[] roles = Roles.GetRolesForUser(username);
                GenericPrincipal prin = new GenericPrincipal(id, roles);
                HttpContext.Current.User = prin;
            }
        }
    }
    

    YourAppIdentity 应该是实现 IIdentity 的东西...

    【讨论】:

    • 不,我意识到这一点。我可能应该在我的示例中使用[Authorize(Users = "DefaultUser")]
    猜你喜欢
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多