R – How to Set the type of the Identity & Principle objects in WPF & WinForms

netwinformswpf

In ASP.Net when you're using Master Pages, you can provide the page a "MasterPage Type" pre-processor command along these lines:

<%@ MasterType VirtualPath="~/MasterPage.master" %>

This is used to provide you access to methods and properties that are defined in the Master page's codebehind. By default, when you use:

Page.MasterPage

it returns the base MasterPage class. If you have the MasterType defined then it returns it as the type of your defined Master Page [as opposed to doing something like: ctype(Page.MasterPage, MyMasterPage)]

My question is, how can I do something similar for the Identity and Principle objects in my WPF application.

Basically, I have custom Identity and Principle objects (which gather a few extra fields of data) and I don't want to have to explicitly cast them from the exposed interface every time I want to access those extra fields.

Best Answer

If you want this available from within your XAML code-behinds, you could inherit from the WPF Window class and add two properties for the Principal and Identity. These properties could be of the type your custom Principal and Identity are. Then just inherit from that class for all of your WPF Windows.

public class AppWindow : Window
{
    public MyPrincipal Principal
    {
        get { return (MyPrincipal)Thread.CurrentPrincipal; }
    }

    public MyIdentity Identity
    {
        get { return (MyIdentity)Thread.CurrentPrincipal.Identity; }
    }
}

If you want to have wider access to these items you could create a simple object with static properties that do the same thing.