【问题标题】:OpenRasta - Scott Littlewoods Basic Authentication working exampleOpenRasta - Scott Littlewoods 基本身份验证工作示例
【发布时间】:2011-05-06 21:06:44
【问题描述】:

我正在测试使用 OpenRasta 作为 ASP.NET MVC 的可行替代方案的可行性。 但是,我遇到了关于身份验证的绊脚石。

让我明确一点,“Open Digest Authentication”目前不是一个选项

我读到 Scott Littlewood 为 OpenRasta 创建了一个基本的身份验证分支,我已经从 git 下载了源代码并成功构建了它。

我现在正试图让身份验证工作,所以如果有人有一个真正的工作模型,我将非常感激。这是我到目前为止所做的:

//Authentication.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OpenRasta;
using OpenRasta.Configuration;
using OpenRasta.Authentication;
using OpenRasta.Authentication.Basic;
using OpenRasta.Configuration.Fluent;
using OpenRasta.DI;

namespace myOpenRastaTest.Extensions
{
    public static class ExtensionsToIUses
    {
        public static void BasicAuthentication<TBasicAuthenticator>(this IUses uses) where TBasicAuthenticator : class, IBasicAuthenticator
        {
            uses.CustomDependency<IAuthenticationScheme, BasicAuthenticationScheme>(DependencyLifetime.Transient);

            uses.CustomDependency<IBasicAuthenticator, TBasicAuthenticator>(DependencyLifetime.Transient);
        }
    }

    public class CustomBasicAuthenticator : IBasicAuthenticator
    {
        public string Realm { get { return "stackoverflow-realm"; } }

        public CustomBasicAuthenticator()
        {            
        }

        public AuthenticationResult Authenticate(BasicAuthRequestHeader header)
        {
            /* use the information in the header to check credentials against your service/db */
            if (true)
            {
                return new AuthenticationResult.Success(header.Username);
            }

            return new AuthenticationResult.Failed();
        }
    }
}

现在为了测试它,我刚刚在 HomeHandler.cs 中创建了一个 CustomBasicAuthenticator 实例:

//HomeHandler.cs
using System;
using myOpenRastaTest.Resources;

namespace myOpenRastaTest.Handlers
{
    public class HomeHandler
    {
        public object Get()
        {
            var custAuth = new myOpenRastaTest.Extensions.CustomBasicAuthenticator();

            return new HomeResource();
        }
    }
}

所以,我需要知道下一步我需要采取哪些步骤,因此我要求一个真正的工作模型,而不仅仅是理论答案,因为我 2 天前刚刚偶然发现了这个框架并且可能不知道所有OpenRasta 框架,RESTful 术语,你可能会扔给我 :)

一旦我掌握了身份验证,我就会很好地了解如何继续评估是否将现有的 asp.net 原型门户移植到 OpenRasta。

提前谢谢...

【问题讨论】:

    标签: rest basic-authentication openrasta


    【解决方案1】:

    我有一个使用新的 OpenRasta 身份验证过程的示例应用程序,目前仅支持 BASIC 身份验证。

    插入不同的身份验证方案应该很简单,但我最近没有时间这样做。

    请参阅此 github 讨论以供将来参考:https://github.com/scottlittlewood/openrasta-stable/commit/25ee8bfbf610cea17626a9e7dfede565f662d7bb#comments

    有关工作示例,请查看此处的代码:https://github.com/scottlittlewood/OpenRastaAuthSample

    希望对你有帮助

    【讨论】:

    • Scott,工作示例是一个完美的示例。非常直接和高效。这正是我所要求的。太感谢了。 --PS。是否有计划为 ASP.NET 实施“表单身份验证”?我已经在期待有关 Forms Auth 有多不安全的回复了 :)
    • 如果您正在寻找 Cookie 身份验证,那么不妨看看 Jamaica ohloh.net/p/jamaica
    【解决方案2】:

    一旦您进行了身份验证,您需要通过对其中一个资源处理程序进行授权来触发它,例如,您可以通过在其上添加 RequiresAuthentication 属性来实现。

    您可以查看该属性的代码,了解如何自己实现自定义授权。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 2021-06-20
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多