简体   繁体   English

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

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

I'm using OWIN based authentication in my MVC application. 我在MVC应用程序中使用基于OWIN的身份验证。 All my controllers and action methods are secured using a custom AuthorizeAttribute. 我所有的控制器和操作方法都使用自定义的AuthorizeAttribute进行保护。

Currently if a user request a page with insufficient permissions (User has no access because he is not in the group which has access to the function) he gets redirected to the login page. 当前,如果用户请求的页面权限不足(用户无权访问,因为他不在有权访问该功能的组中),他将被重定向到登录页面。 This behavior could confuse the user because he doesn't know why he was redirected to the login page. 此行为可能会使用户感到困惑,因为他不知道为什么将他重定向到登录页面。

Is it possible to display a message in this case? 在这种情况下可以显示消息吗? …Can OWIN add a parameter to the login-page request (something like the ReturnUrl parameter)? …OWIN可以向登录页面请求添加参数(类似于ReturnUrl参数)吗?

Thanks for your help. 谢谢你的帮助。

Edit: 编辑:

[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 )
    }
}

Use can use session store. 使用可以使用会话存储。 I usually store messages to be displayed on the next screen display in a session variable (as a list). 我通常将要在下一个屏幕显示中显示的消息存储在会话变量(作为列表)中。 The layout page is then responsible to display an appropriate message: 然后,布局页面负责显示适当的消息:

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

with code in Views/Account/Login.cshtml: 在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>
}

With all the rest here . 剩下的一切都在这里

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

相关问题 ASP.Net 双 OWIN 身份验证 - 如何防止非 AD 用户重定向到 AzureAD 登录 - ASP.Net dual OWIN authentication - how to prevent redirect to AzureAD-login for non-AD-users ASP.Net MVC-OWIN-允许Windows /个人身份验证 - ASP.Net MVC - OWIN - Allowing Windows/Individual Authentication 如何在没有ASP.NET和MVC的情况下在OWIN中创建身份验证 - How to create authentication in owin without ASP.NET and without MVC 使用Owin中间件进行Active Directory身份验证的ASP.Net MVC - ASP.Net MVC with Active Directory Authentication using Owin Middleware Asp.net 5 MVC6 - 使用owin登录facebook - Asp.net 5 MVC6 - Using owin for facebook login ASP.NET MVC 和登录身份验证 - ASP.NET MVC and Login Authentication ASP.Net MVC 3登录和Windows身份验证 - ASP.Net MVC 3 Login and Windows Authentication ASP.NET MVC 5.1 C#OWIN facebook身份验证或登录请求生日,喜欢,公开个人资料,电话号码 - ASP.NET MVC 5.1 C# OWIN facebook authentication or login ask for birthday, likes, public profile, phone number 用户路由到关键区域时,ASP.NET MVC身份验证重定向到登录页面 - ASP.NET MVC Authentication Redirect to Login Page when user route to the critical area Asp.Net WebApi OWIN身份验证 - Asp.Net WebApi OWIN Authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM