MVC Core Session Authorization or Session Authorization or Session Authorize or or Core Session Authorization
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication6.Auth
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class CustomAuthorization : Attribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationFilterContext filterContext)
{
var ses = filterContext.HttpContext.Session.GetString("userid");
if (ses == null || ses == "")
{
//filterContext.HttpContext.Response.Redirect("/Home/Login");
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
//{"Controller","Login" }
{"Controller","Login" },
{"Action","Index" }
}
);
return;
}
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class CustomAuthorizationRole : Attribute, IAuthorizationFilter
{
string forms_; string actions_;
public CustomAuthorizationRole(string forms, string actions)
{
forms_ = forms;
actions_ = actions;
}
public void OnAuthorization(AuthorizationFilterContext filterContext)
{
if (forms_.ToLower() == "login" && actions_.ToLower() == "view") { }
else
{
//filterContext.HttpContext.Response.Redirect("/Home/Login");
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{"Controller","Home" },
{"Action","AccessDinied" }
}
);
return;
}
}
}
}
No comments:
Post a Comment