繁体   English   中英

ASP.Net MVC 5-OWIN身份验证登录重定向显示消息

[英]ASP.Net MVC 5 - OWIN Authentication login redirect display message

我在MVC应用程序中使用基于OWIN的身份验证。 我所有的控制器和操作方法都使用自定义的AuthorizeAttribute进行保护。

当前,如果用户请求的页面权限不足(用户无权访问,因为他不在有权访问该功能的组中),他将被重定向到登录页面。 此行为可能会使用户感到困惑,因为他不知道为什么将他重定向到登录页面。

在这种情况下可以显示消息吗? …OWIN可以向登录页面请求添加参数(类似于ReturnUrl参数)吗?

谢谢你的帮助。

编辑:

[AttributeUsage( AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false )]
public class FunctionAuthorizeAttribute : AuthorizeAttribute
{
    public IUserInformationService UserInformationService { get; set; }
    public IGenericParameterService GenericParameterService { get; set; }
    public String FunctionName { get; set; }

    public FunctionAuthorizeAttribute( String functionName )
    {
        FunctionName = functionName;
    }

    protected override Boolean AuthorizeCore( HttpContextBase httpContext )
    {
        var parameter = GenericParameterService.GetCommonParameters();
        if ( !parameter.CacheSecurityParameter )
            parameter = GenericParameterService.GetCommonParameters(false);

        return !parameter.EnableAuthentication || UserInformationService.HasAccess( FunctionName )
    }
}

使用可以使用会话存储。 我通常将要在下一个屏幕显示中显示的消息存储在会话变量(作为列表)中。 然后,布局页面负责显示适当的消息:

SessionMessage.AddMessage("Huh - you should login first", SessionMessage.MessageType.warning);

在Views / Account / Login.cshtml中添加代码:

@foreach (SessionEntry item in SessionMessage.GetSessionEntries())
{
   <div class="alert alert-@(Enum.GetName(typeof(SessionMessage.MessageType), item.MessageType).ToLower())">
       <button type="button" class="close" data-dismiss="alert"></button>
       @item.Message
       </div>
}

剩下的一切都在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM