我如何在ASP.NET中实现MVC自定义主体和身份? [英] How do I implement custom Principal and Identity in ASP.NET MVC?
问题描述
我要存储额外的信息在身份验证的用户,这样我可以把它方便(如User.Identity.Id,例如),而不是仅仅的名字,因为我打算在具有非唯一。
I want to store extra information in the authenticated user so that I can have it easily accessible (like User.Identity.Id, for example), instead of just the name, since I'm planning on having that non-unique.
到目前为止,我收集了,我应该去实现自定义的校长和/或身份,但我不知道如何去做。我一直在寻找文档和教程就此事,但我发现相关的东西在不同的地方,我发现它有点混乱。
So far I've gathered that I should look to implement custom Principal and/or Identity, but I'm not sure how to go about it. I've been looking for documentation and tutorials on the matter, but I've found related stuff in different places and I've found it a bit confusing.
我已经看到了如何自定义信息添加到用户数据属性身份验证cookie,但我想有依赖注入的单元测试,我可以有校长和身份的好处。
I have seen how to add custom info to the authentication cookie in the user data property, but I'd like to have the benefit of dependency injection for unit testing, which I can have with the principal and identity.
什么 确切的步骤,我需要考虑如果我想实现我自己的主体或身份?
What are the exact steps I need to consider if I want to implement my own Principal or Identity?
什么是我可以在这种情况下做的最简单(只需添加的ID,并保持所有默认设置的地方)? 默认值将包括默认的提供者(成员,角色等)。
What would be the simplest I could do in this scenario (just add the ID and keep all the defaults in place)? "Defaults" would include the default providers (membership, roles, etc.).
我见过的<一个href=\"http://stackoverflow.com/questions/1064271/asp-net-mvc-set-custom-iidentity-or-iprincipal\">other问题,但我AP preciate答案不留下任何孔之间,如魔的角色在字符串中的示例中的AuthenticateRequest事件。相反,我需要知道如何从默认SqlRoleProvider添加的角色给当前用户:何时何地做,如果我需要做任何事情对我的新类与其他默认提供连接。
I have seen the other question, but I'd appreciate answers that do not leave any holes in between, such as the roles magic strings in the AuthenticateRequest event in the examples. Instead I need to know how to add the roles from the default SqlRoleProvider to the current user: when and where to do it, and if I need to do anything else to connect my new classes with the other default providers.
这将会是真棒,能够去一个样本ASP.NET MVC 2应用程序(从Visual Studio 2010的模板,例如),使编辑和它的工作。
It'd be awesome to be able to go to a sample ASP.NET MVC 2 application (from the visual studio 2010 template, for example), make the edits and have it work.
编辑:我编辑的问题,以更好地展示我是pretty多少 失去的在这里,这样我就可以' ŧ凑合着过高水平的答案。
I have edited the question to better show that I'm pretty much lost here, so I can't make do with too high level answers.
P.S:在我看来,它更有意义,有身份,而不是主要的ID,我虽在某种程度上,说明在此之前
P.S.: It seems to me that it makes more sense to have the ID in the Identity instead of the principal, although I have, in a way, stated this before.
推荐答案
这个问题已经被问和回答过:
<一href=\"http://stackoverflow.com/questions/1064271/asp-net-mvc-set-custom-iidentity-or-iprincipal\">http://stackoverflow.com/questions/1064271/asp-net-mvc-set-custom-iidentity-or-iprincipal
This question has been asked and answered before: http://stackoverflow.com/questions/1064271/asp-net-mvc-set-custom-iidentity-or-iprincipal
但总结...
箱子你想存储额外的性质在一个自定义的主类:
Crate a custom principal class with the additional properites you want to store:
Public Class CustomPrincipal
Inherits System.Security.Principal.GenericPrincipal
Private _eyeColor As String
Public ReadOnly Property EyeColor As String
Get
Return _eyeColor
End Get
End Property
Public Sub New(id As System.Security.Principal.IIdentity, roles As String(), eyeColor As String)
MyBase.New(id, roles)
_eyeColor = eyeColor
End Sub
End Class
修改Global.asax的Global.Application_AuthenticateRequest使用您的自定义主体:
Modify global.asax Global.Application_AuthenticateRequest to use your custom principal:
Protected Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As System.EventArgs)
...
Dim roles() As String = {"examplerole"}
Context.User = new CustomPrincipal(Context.User.Identity, roles, "green")
End Sub
然后在你的code其他地方,当你想引用这些属性之一做到这一点:
Then elsewhere in your code when you want to refer to one of these properties do this:
CType(My.User.CurrentPrincipal, CustomPrincipal).EyeColor
这篇关于我如何在ASP.NET中实现MVC自定义主体和身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!