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