- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力找出哪些 .Net 身份验证概念在 OWIN 世界中仍然相关,哪些现在已经过时。从 OWIN 之前的 ASP.Net 时代开始,我就习惯于处理 .Net 构造:FormsAuthentication、FormsAuthCookie、IPrincipal、IIdentity 以及 IPrincipal 的自定义实现(继承自 GenericPrincipal)。在最新版本的 MVC (5) 中,大部分身份验证似乎已更改为基于 OWIN。我特别想了解两件事:
1) IPrincipal 和 IIdentity 以及 GenericPrincipal 适合什么位置?使用 FormsAuthentication,自定义数据可以存储在 FormsAuth cookie 中。然后可以在 ASP.Net PostAuthenticate 事件中使用它来创建 CustomPrincipal 对象,并覆盖 HTTPContext 上的默认 IPrincipal(根据下面的代码示例)。 OWIN 如何(或如何)改变这一点?:
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
//Decrypt forms authentication cookie and retrieve some userdata
...
//Create CustomPrincipal (which inherits from GenericPrincipal)
var principal = new CustomPrincipal(userId, roles, someAdditionalUserDataFromCookie);
//Replace standard IPrincipal object on HTTPContext with custom principal
HttpContext.Current.User = newUser
}
2) 自定义身份验证数据可以存储在哪里? 在 OWIN 出现之前,我使用 AuthCookie 的 UserData 值来存储自定义身份信息(除了用户名之外) - 例如 OrgID 。现在可以将其作为声明存储在 ClaimsIdentity 对象中吗?这是一个好主意吗?它仍然可以存储在 AuthenticationTicket 中吗?我是不是看错了?!
感谢您的帮助。
最佳答案
您将使用 CookieAuthenticationMiddleware
而不是 FormsAuthenticationModule
。 CookieAuthenticationMiddleware
仍然使用身份验证票证创建 cookie,但格式不同。使用CookieAuthenticationMiddleware
,一切都是从头开始为声明而设计的。因此,默认情况下,您会获得带有 ClaimsIdentity
的 ClaimsPrincipal
,尽管这些类实现了 IPrincipal
和 IIdentity
。
关于自定义身份验证数据,请将它们存储为身份的声明部分。新世界的一大好处是,您不再需要使用 PostAuthenticate
来根据票证中的自定义数据恢复您的主体。如果您在调用 SignIn
之前使用所有必需的声明创建身份,CookieAuthenticationMiddleware
会负责将身份的声明部分序列化到 cookie 中的票证中,然后返回到其身份中的身份。整体。此外,您不会使用 HttpContext.Current.User 来读取主体。您将使用请求对象上可用的扩展方法从 OWIN 上下文中读取内容,如下所示。
Request.GetOwinContext().Authentication.User
返回 ClaimsPrincipal
Request.GetOwinContext().Request.User
返回与上面相同的结果,但与 IPrincipal
在 Controller 中,您可以使用 User
(即 IPrincipal
),它再次从上下文中返回一个。
关于asp.net - IIdentity、IPrincipal、OWIN、IdentityUser 和 IUser<string> 如何组合在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27392677/
================================= 现在我的登录页面有问题; Vector 无法转换为 IUser 我的无状态Bean /* * To change this tem
我正在尝试在 MVC5 应用程序中实现简单的基于角色的身份验证 + 授权,但我在尝试理解身份框架中涉及的所有部分时感到有些头疼 我正在阅读一些教程和指南,但我仍然没有一个清晰的想法。 特别是:IIde
我正在尝试为我的 Web 应用程序实现一个带有电子邮件确认的注册过程。我正在使用 IdentityServer3.AspNetIdentity 服务。 public class AspNetIdent
我正在尝试在身份成员身份的自定义实现这个黑洞中导航。我现在的目标只是从我的 ApiController 中获取此行以正确检索我的 UserManager: public IHttpActionResu
我正在尝试根据现有数据库对用户进行身份验证。我发现我需要实现 IUserStore。我正在尝试创建实现 IUser 的自定义用户,我可以发现 IUser 位于 Microsoft.AspNet.Ide
我有这个代码: public class BaanUserStore : IUserStore, IUserPasswordStore where TUser : Microsoft.AspNet.I
在我的 Api 服务中,我有这个简单的 getUsers 函数来获取 API 上的所有用户。 public getUsers(url: string): Observable { return
我正在努力找出哪些 .Net 身份验证概念在 OWIN 世界中仍然相关,哪些现在已经过时。从 OWIN 之前的 ASP.Net 时代开始,我就习惯于处理 .Net 构造:FormsAuthentica
我正在使用 Asp.Net Identity 框架,我有一个如下所示的用户类: public class User : IUser { public string Id {get; privat
我们正在开发一个使用 ASP.NET Identity(v 1.0)的应用程序。我们定义了一个实现 IUser 接口(interface)的 User 类。我们的存储库类 UserRepository
我是一名优秀的程序员,十分优秀!