ASP.NET MVC - Set custom IIdentity or IPrincipal

后端 未结 9 1390
忘了有多久
忘了有多久 2020-11-21 23:06

I need to do something fairly simple: in my ASP.NET MVC application, I want to set a custom IIdentity / IPrincipal. Whichever is easier / more suitable. I want to extend the

9条回答
  •  隐瞒了意图╮
    2020-11-22 00:02

    Here is a solution if you need to hook up some methods to @User for use in your views. No solution for any serious membership customization, but if the original question was needed for views alone then this perhaps would be enough. The below was used for checking a variable returned from a authorizefilter, used to verify if some links wehere to be presented or not(not for any kind of authorization logic or access granting).

    using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Security.Principal;
    
        namespace SomeSite.Web.Helpers
        {
            public static class UserHelpers
            {
                public static bool IsEditor(this IPrincipal user)
                {
                    return null; //Do some stuff
                }
            }
        }
    

    Then just add a reference in the areas web.config, and call it like below in the view.

    @User.IsEditor()
    

提交回复
热议问题