Thinktecture.IdentityModel with SimpleMembership

时间:2012-12-14 22:09:13

标签: asp.net asp.net-mvc asp.net-web-api authentication thinktecture-ident-model

我想使用ASP.NET SimpleMembership来验证使用我的WebAPI的用户。 Thinktecture有一个名为Thinktecture.IdentityModel(http://thinktecture.github.com/Thinktecture.IdentityModel.45/)的精彩认证库,其中包含一个将Forms Auth与Basic Auth(source)联系起来的示例。但是,该示例使用Membership.ValidateUser(),如果没有ASP.NET成员资格提供程序,则不能使用,而SimpleMembership(source)不支持(编辑:这不是'完全正确,见马克的答案。)


修改

这就是我的所作所为:

1)创建一个新的MVC Internet应用程序

2)通过NuGet安装Thinktecture.IdentityModel

3)通过脚手架创建模型和api控制器:

public class Goober
{
    public int GooberId { get; set; }
    public string GooberWords { get; set; }
}

4)跑完项目,创建一个新用户并使用Fiddler创建一个新的Goober

5)将[授权]添加到GetGoober(int id)

6)在WebApiConfig.cs中添加:

var authConfig = new AuthenticationConfiguration();

authConfig.AddBasicAuthentication((userName, password) =>
    Membership.ValidateUser(userName, password));

config.MessageHandlers.Add(new AuthenticationHandler(authConfig));

当我运行该项目并用Fiddler命中api / goober / 1时,我得到一个 401 www-Authenticate:unspecified 。但是,如果我首先使用AccountController登录然后使用Fiddler,我会得到 200 ,一切都很好。

修改

好的,我认为问题与我最初的问题无关。我怀疑它与模板中SimpleMembership的初始化有关。当我打开项目并运行调试然后用Fiddler点击api我无法通过Auth。但是当我只是点击网络前端的“注册”链接时,我就会通过Auth。我猜是因为在AccountController中调用了InitializeSimpleMembershipAttribute,所以在调用控制器之前不会初始化吗?


我尝试在Membership.ValidateUser()的位置使用WebSecurity.Login(),但这不起作用。

我对如何实际实现这一点感到茫然。有人有建议吗?或者我可能试图从错误的角度解决这个问题?

1 个答案:

答案 0 :(得分:3)

您是正确的,ASP.NET成员资格提供程序与SimpleMembershipProvider不兼容,但SimpleMembershipProvider支持ValidateUser,请参阅here。假设SimpleMembership配置正确并且已经初始化,您仍然可以调用Membership.ValidateUser()

如果您已尝试过Membership.ValidateUser()并收到错误,请告知我们,我们可以尝试解决此问题。

<强>更新

因此,按照您的再现步骤,我设法指出了一个错误。将Thinktecture AuthenticationHandler处理程序内联并在调试中运行。在向api控制器发出请求30秒后,正在引发数据库连接错误。这是异步和静默失败。

经过一番摆弄后,我相信这是DefaultConnection连接字符串有问题。

与我一样,您的默认连接可能包含如下文件名:     AttachDBFilename = | DataDirectory目录| \ ASPNET-MvcApplication3-20121215104323.mdf“

当在app启动时注册的委托内调用ValidateUser时(用于验证凭据),它似乎无法解析|DataDirectory|我发现通过将此路径更新为全名,我的连接问题就消失了。

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication3-20121215104323;Integrated Security=SSPI;AttachDBFilename=C:\mydatabase\file\path\example\aspnet-MvcApplication-20121215104323.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>

然后我找到了这篇帖子here,它表明AppDomain此时没有正确设置它的数据目录。

所以一旦配置设置和连接字符串改变了正确的文件路径和用户名“test”和密码“测试”这个请求通过fiddler获得了200:

GET http://localhost/api/goober/1
User-Agent: Fiddler
Host: localhost
Authorization: Basic dGVzdDp0ZXN0aW5n

撇开

我发现要获取表单授权令牌以允许访问api控制器,我必须添加它。否则,Thinkteckture代码将原则设置为匿名:

添加此

authConfig.InheritHostClientIdentity = true;

抵消this(第52行):

if (_authN.Configuration.InheritHostClientIdentity == false)
{
   //Tracing.Information(Area.HttpAuthentication, "Setting anonymous principal");
   SetPrincipal(Principal.Anonymous);
}