- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 ASP.NET SimpleMembership 对使用我的 WebAPI 的用户进行身份验证。 Thinktecture 有一个很棒的身份验证库,名为 Thinktecture.IdentityModel (http://thinktecture.github.com/Thinktecture.IdentityModel.45/),其中有一个将 Forms Auth 与 Basic Auth 联系起来的示例 ( source )。但是,该示例使用 Membership.ValidateUser(),如果没有 ASP.NET 成员(member)资格提供程序,该提供程序将无法工作,而 SimpleMembership ( source ) 不支持该提供程序(编辑:这并不完全是是的,请参阅下面马克的回答)。
<小时/>编辑:
这就是我所做的:
1) 创建一个新的 MVC Internet 应用程序
2) 通过 NuGet 安装 Thinktecture.IdentityModel
3)通过脚手架创建模型和api Controller :
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 时,我无法通过身份验证。但是,当我只需单击网络前端上的“注册”链接时,我就通过了身份验证。我猜这是因为在 AccountController 中调用了 InitializeSimpleMembershipAttribute,所以在调用 Controller 之前不会初始化?
<小时/>我尝试使用 WebSecurity.Login() 代替 Membership.ValidateUser(),但这不起作用。
我不知道如何实际实现这一点。有人有建议吗?或者也许我试图从错误的角度解决这个问题?
最佳答案
您说得对,ASP.NET 成员(member)提供程序与 SimpleMembershipProvider
不兼容,但 SimpleMembershipProvider
确实支持 ValidateUser
,请参阅 here 。假设 SimpleMembership 已正确配置并初始化,您仍然应该能够调用 Membership.ValidateUser()
。
如果您已尝试 Membership.ValidateUser()
并收到错误,请告诉我,我们可以尝试解决该问题。
更新
因此,按照您的重现步骤,我成功地找出了一个错误。将 Thinktecture AuthenticationHandler
处理程序内联并在调试中运行。向 api Controller 发出请求后 30 秒,将引发数据库连接错误。这是异步且无提示地失败。
经过一番摆弄后,我相信这是 DefaultConnection
连接字符串有问题。
像我一样,您的默认连接可能包含如下文件名: AttachDBFilename=|DataDirectory|\aspnet-MvcApplication3-20121215104323.mdf"
当在应用程序启动时注册的委托(delegate)内部调用 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”和密码“testing”更改了连接字符串,通过 fiddler 的此请求就会得到 200:
GET http://localhost/api/goober/1
User-Agent: Fiddler
Host: localhost
Authorization: Basic dGVzdDp0ZXN0aW5n
作为旁白
我发现要获取表单授权 token 以允许访问 api Controller ,我必须添加它。否则,Thinkteckture 代码将原则设置回匿名:
添加这个
authConfig.InheritHostClientIdentity = true;
抵消this (第 52 行):
if (_authN.Configuration.InheritHostClientIdentity == false)
{
//Tracing.Information(Area.HttpAuthentication, "Setting anonymous principal");
SetPrincipal(Principal.Anonymous);
}
关于asp.net - Thinktecture.IdentityModel 与 SimpleMembership,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13886958/
我正在使用 Forms Authentication 和 ASP.NET MVC 2 开发一个基于声明的授权系统。我看到一个命名空间“Microsoft.IdentityModel”提供了很多与另一个
我在使用 System.IdentityModel.Tokens 时遇到冲突: using System; using System.Configuration; using System.Data;
我可能不是第一个问但我们现在应该使用哪个?据我了解,它从一个非常小的 开始。系统 .IdentityModel 用于 WCF。然后是微软 .IdentityModel 类增加了很多。这是 http:/
我开始在我的项目中使用 ThinkTecture.IdentityModel,但我遇到了一个严重的问题。请帮助我。 错误 1“类型‘System.IdentityModel.Tokens.Securi
在遵循教程时 Create a RESTful API with authentication using Web API and Jwt我在编译 CustomJwtFormat 类时遇到问题: us
我们可以在 NuGet 库中找到两个独立的包: System.IdentityModel.Tokens.Jwt Microsoft.IdentityModel.Tokens 两者似乎都会定期更新并拥有
使用 .net 4.7 和 IdentityModel 的 Windows 服务应用程序。这是一个 Windows 服务:它在我的机器 (Windows 10) 上运行良好,但在 Windows Se
我是 .NET 的新手,目前正在通过关注 this tutorial 学习 ASP.NET Core MVC . 我的开发环境: Ubuntu 16.04.1 LTS .Net Core 1.0.0-
构建时我不断收到此错误: 无法识别的配置部分 system.identityModel 我已将以下内容添加到我的配置中:
我正在尝试在SharePoint Foundation 2010项目中使用 SPFederationAuthenticationModule 类。\我找不到 Microsoft.Sharepoint.
我已经更新了 System.IdentityModel.Tokens.jwt,它警告我在较低的代码块中使用“Audience”已过时。我做了一些研究,发现让观众前进的正确方法是: var valida
我已经更新了 System.IdentityModel.Tokens.jwt,它警告我在较低的代码块中使用“Audience”已过时。我做了一些研究,发现让观众前进的正确方法是: var valida
我已安装 Windows Identity Foundation,但找不到 Microsoft.IdentityModel dll。根据 Azure 动手实验室,它应该只是在 VS2010 中的“添加
我有一个使用 IdentityModel 中的 TokenClient 的新 MVC 项目 var tokenClient = new TokenClient(tokenUrl, clientId,
我正在尝试通过实现 UserNamePasswordValidator 来提供用户身份验证。我添加了 Microsoft.IdentityModel 和 System.IdentityModel.To
我正在尝试通过自定义类库使用 IdentityModel.dll。 我有一个 Web 服务(将作为 WSP 部署在 SharePoint 中),我想在其中使用 IdentityModel.Oidccl
如 leastprivilege 中所述,有两种方法可以使用 Thinktecture.IdentityModel 设置声明授权检查。一种是设置过滤器。另一种是为要检查的 Action 添加属性。 我
我有一个基于 TcpClient/TcpListener 和 SslStream 的简单客户端-服务器应用程序。客户端可以使用 X509Certificate 或在建立 SslStream 后发送用户
我最近被迫将我的 System.IdentityModel.Tokens.Jwt NuGet 包更新到 5.1.4,因为另一个 NuGet 包。更改后的大部分代码似乎很容易解决,但现在 Configu
我正在尝试使用 JWT token 验证用户。我在下面使用的代码在控制台应用程序中工作得很好。但是当我想在我的 Azure 函数中应用它时,它给我错误: 无法加载文件或程序集 Microsoft.Id
我是一名优秀的程序员,十分优秀!