problem in use of ASP.NET MVC Default Form Auth and Our Form Auth together

Viewed 38

i use asp .net authentication method in my project, for users login...

Web.config file contain this lines for this approach...

.
.
.
  <system.web>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2" />
    <authentication mode="Forms">
        <forms name="my_web_project" defaultUrl="/Login/LoginForm" loginUrl="~/Login/LoginForm" timeout="10" />
    </authentication>
    <customErrors mode="Off" />
      <sessionState cookieName="ASP.NET_SessionId" mode="InProc" timeout="10" />
      [or]
      <sessionState cookieName="ASP.NET_SessionId" />
      [not different between them in my code!!!]
  </system.web>
 .
 .
 .

Login Action Method in LoginController.cs file contain this lines for this approach...

.
.
.

    // microsoft method
    FormsAuthentication.SetAuthCookie(login.UserName, login.RememberMe);

.
.
.
    
    // my method
    HttpCookie logoutCookie = new HttpCookie("logoutCookie", "");
    logoutCookie.Expires = DateTime.Now.AddMinutes(10);
    Response.Cookies.Add(logoutCookie);
    
    // my method
    HttpCookie loginCookie = new HttpCookie("loginCookie", "[a hash code]");
    loginCookie.Expires = DateTime.Now.AddMinutes(10);
    Response.Cookies.Add(loginCookie);
    
.
.
.

Signout Action Method in LoginController.cs file contain this lines for this approach...

.
.
.
    TempData.Clear();
    Session.Clear();
    Session.Abandon();
    FormsAuthentication.SignOut();
    Response.Cookies.Clear();
    if (Request.Cookies["my_web_project"] != null)
    {
        HttpCookie nameCookie = Request.Cookies["my_web_project"];
        nameCookie.Expires = DateTime.Now.AddDays(-1);
        Response.Cookies.Add(nameCookie);
    }
.
.
.

MyCustomAPIAuthFilter.cs is contain these lines...

.
.
.

namespace NiktavWebProject.CustomFilters
{
    public class CustomApiAuthFilter : AuthorizationFilterAttribute
    {
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            var cookie = actionContext.Request.Headers.GetCookies("session").FirstOrDefault();

            var controllerName = actionContext.ControllerContext.RouteData.Values["controller"].ToString();

            bool flagMustLogin = false;

            if (controllerName != "AuthApi")
            {
                if (cookie != null)
                {
                    var loginCookie = cookie.Cookies[0].Values["loginCookie"];

                    if (loginCookie == null)
                    {
                        flagMustLogin = true;
                    }
                }
                else
                {
                    flagMustLogin = true;
                }
            }
.
.
.

MyCustomAuthFilter.cs file contain these lines...

.
.
.
namespace NiktavWebProject.CustomFilters
{
    public class CustomAuthFilter : ActionFilterAttribute, IAuthenticationFilter
    {
        private string loginPath = "/Login/LoginForm";

        public void OnAuthentication(AuthenticationContext filterContext)
        {
            if (filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
.
.
.

use see that I use my authentication method using cookies...

Problem is that its a conflict between these methods... how can i use my customized authentication method near by asp.net mvc standard method... and is a right way? is any better way?

thanks so much...

0 Answers
Related