How do I implement custom Principal and Identity in ASP.NET MVC?

喜夏-厌秋 提交于 2019-11-30 00:25:18
Seth Reno

This question has been asked and answered before: ASP.NET MVC - Set custom IIdentity or IPrincipal

But to summarize...

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

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

Then elsewhere in your code when you want to refer to one of these properties do this:

CType(My.User.CurrentPrincipal, CustomPrincipal).EyeColor

You can't realy expect that someone can teach you everything you don't know about .NET in a few paragraphs. You can read pretty good example at MSDN http://msdn.microsoft.com/en-us/library/system.security.principal.genericprincipal.aspx and dig through the class and it's derivatives in Reflector - there's nothing spectacularly special about it.

Roles are just a string arrray of names for your own use, in your app/server.

Having said that, you don't really have to aim at exact GenericPrincipal derrivative at all. Check out

HttpContext.Current.Items

It's a Hashtable for free use just for the request you are servicing - meaning that at one point you can do say:

HttpContext.Current.Items["TokenUser"] = new MyThinUser(anything,I,want,there);

and then everythere else in the code just do:

var user = HttpContext.Current.Items["TokenUser"] as MyThinUser;

and you are done.

Store in your new class everything you need/want to pass around from the authentification code to all other functions. Leaves User property intact (so you can peek into it and not have to worry that you changed something). Wihr that you can simplify or complicate your system at will but you keep full independence.

For example if you have your own authentification and just a few levels of acces instead of enumerated roles you can just carry good old access level number (enumerated roles carried around as strings are very inefficient anyway).

Keep in mind that autogenned samples in VS are usually geared towards some particular scenario. So if you see SQL providers for user management that doesn't mean that you actually have to use it - you can still just call your own sproc to get what you need from your own table in SQL.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!