Implement custom iprincipal in mvc

8
How to implement a custom IPrincipal in ASP.NET MVC Nat 20140224

Transcript of Implement custom iprincipal in mvc

Page 1: Implement custom iprincipal in mvc

How to implement a custom IPrincipal in

ASP.NET MVCNat

20140224

Page 2: Implement custom iprincipal in mvc

Why ?

We want more user information in Principal object

Ex.

User.MemberID

User.LoginID

Page 3: Implement custom iprincipal in mvc

IPrincipal and IIdentity

System.Security.Principal.IPrincipal

Page.User

Controller.User

System.Security.Principal.IIdentity

User.Identity

Page 4: Implement custom iprincipal in mvc

How

Define a class which implements IPrinicpal interface

Save custom settings to Cookie

Restore custom settings on every request

Replace User object on PostAuthenticateRequest event

Page 5: Implement custom iprincipal in mvc

Implementation

public class CustomPrincipalSerializeModel

public class CustomPrincipal : IPrincipal

Signin – save custom settings

Login action

Request – restore custom settings

Global.asax - Application_PostAuthenticateRequest

Page 6: Implement custom iprincipal in mvc

Implementation

Replace (User as CustomPrincipal).LoginID with User.LoginID

public class CustomController : Controller

Page 7: Implement custom iprincipal in mvc

Implementation

Replace @(User as CustomPrincipal).MemberID With @(User.MemberID)

public abstract class CustomWebViewPage : WebViewPage

public abstract class CustomWebViewPage<TModel> : WebViewPage<TModel>

Views/Web.config

<pages pageBaseType="CustomWebViewPage">