Implement custom iprincipal in mvc
-
Upload
learningtech -
Category
Technology
-
view
748 -
download
3
Transcript of Implement custom iprincipal in mvc
How to implement a custom IPrincipal in
ASP.NET MVCNat
20140224
Why ?
We want more user information in Principal object
Ex.
User.MemberID
User.LoginID
IPrincipal and IIdentity
System.Security.Principal.IPrincipal
Page.User
Controller.User
System.Security.Principal.IIdentity
User.Identity
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
Implementation
public class CustomPrincipalSerializeModel
public class CustomPrincipal : IPrincipal
Signin – save custom settings
Login action
Request – restore custom settings
Global.asax - Application_PostAuthenticateRequest
Implementation
Replace (User as CustomPrincipal).LoginID with User.LoginID
public class CustomController : Controller
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">
Reference
http://www.codeproject.com/Tips/574576/How-to-implement-a-custom-IPrincipal-in-ASP-NET-MV
http://stackoverflow.com/questions/1064271/asp-net-mvc-set-custom-iidentity-or-iprincipal