# Problem with Authentication

**URL:** <https://forum.dotvvm.com/t/problem-with-authentication/229>\
**Category:** Discussion in English\
**Created:** [July 9, 2024, 2:31pm UTC](https://forum.dotvvm.com/t/problem-with-authentication/229 "2024-07-09T14:31:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dosmac](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/d/edb3f5/32.png) [@Dosmac](https://forum.dotvvm.com/u/Dosmac)\
**Post date:** [July 9, 2024, 2:31pm UTC](https://forum.dotvvm.com/t/problem-with-authentication/229/1 "2024-07-09T14:31:46Z")

</div>

Hello, I followed the examples in these two pages to create a very simple authentication mechanism.

> **[Overview | DotVVM Documentation](https://www.dotvvm.com/docs/4.0/pages/concepts/security/authentication-and-authorization/overview)**
>
> We have documentation for all DotVVM controls, including the commercial ones, ... Free online theme creator and style editor for the DotVVM Business Pack.

> **[ASP.NET Core | DotVVM Documentation](https://www.dotvvm.com/docs/4.0/pages/concepts/security/authentication-and-authorization/aspnetcore)**
>
> We have documentation for all DotVVM controls, including the commercial ones, ... Free online theme creator and style editor for the DotVVM Business Pack.

The authentication works fine when I add

`Context.Authorize();`

to the Init() method of each ViewModel independently  
However, when I only add it to the MasterPage ViewModel in order to require authentication over all my pages I instead get a very strange behavior, where I am getting a page loading while redirects are added to the url resulting in something like this:

![image](https://forum.dotvvm.com/uploads/default/original/1X/54cf35d1e0a9ba6985c63ae220d67fcbb9866947.png)

going on for many lines, but you get the point.  
Any ideas as to why that might be? I’ve never worked with Authentication before so any ideas are welcome!

---

<div class="post-metadata">

**Author:** ![exyi](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/exyi/32/9_2.png) [@exyi](https://forum.dotvvm.com/u/exyi)\
**Post date:** [July 9, 2024, 2:52pm UTC](https://forum.dotvvm.com/t/problem-with-authentication/229/2 "2024-07-09T14:52:18Z")

</div>

At first glance, it seems like you are also calling Context.Authorize() on the login page, getting you into a redirect loop.

---

<div class="post-metadata">

**Author:** ![Dosmac](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/d/edb3f5/32.png) [@Dosmac](https://forum.dotvvm.com/u/Dosmac)\
**Post date:** [July 10, 2024, 8:23am UTC](https://forum.dotvvm.com/t/problem-with-authentication/229/3 "2024-07-10T08:23:20Z")

</div>

I also looked for something like that at first, but on my Login.dothtml I only have this:

```auto
public override Task Init()
{
   
    return base.Init();
}

```

```auto
 services.AddAuthentication(sharedOptions =>
 {
     sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
 })
     .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
     {
         options.Events = new CookieAuthenticationEvents
         {
             OnRedirectToReturnUrl = c => DotvvmAuthenticationHelper.ApplyRedirectResponse(c.HttpContext, c.RedirectUri),
             OnRedirectToAccessDenied = c => DotvvmAuthenticationHelper.ApplyStatusCodeResponse(c.HttpContext, 403),
             OnRedirectToLogin = c => DotvvmAuthenticationHelper.ApplyRedirectResponse(c.HttpContext, c.RedirectUri),
             OnRedirectToLogout = c => DotvvmAuthenticationHelper.ApplyRedirectResponse(c.HttpContext, c.RedirectUri)
         };
         options.LoginPath = "/login";
     });

```

this is my Authentication service on Startup.cs copied directly from the documentation.

and this is the way I add my Login page in DotvvmStartup.cs in case this helps at all

```auto
config.RouteTable.Add("Login", "login", "Views/Login.dothtml");

```

---

<div class="post-metadata">

**Author:** ![exyi](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/exyi/32/9_2.png) [@exyi](https://forum.dotvvm.com/u/exyi)\
**Post date:** [July 10, 2024, 1:50pm UTC](https://forum.dotvvm.com/t/problem-with-authentication/229/4 "2024-07-10T13:50:48Z")

</div>

I see, but I suppose that the loop will still happen, if the LoginViewModel inherits from the base class where you call `Context.Authorize()`

---

<div class="post-metadata">

**Author:** ![Dosmac](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/d/edb3f5/32.png) [@Dosmac](https://forum.dotvvm.com/u/Dosmac)\
**Post date:** [July 10, 2024, 2:10pm UTC](https://forum.dotvvm.com/t/problem-with-authentication/229/5 "2024-07-10T14:10:14Z")

</div>

I see, that does make sense. However, according to the documentation:

> If you want the same permission check for all pages, you can place the Authorize method call to the master page viewmodel. If you override the Init method in page viewmodels, make sure to properly call await base.Init(context); so the check is applied.

What other steps should I take to state that the /login url does not need to be authenticated so to resolve this problem?

---

<div class="post-metadata">

**Author:** ![exyi](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/exyi/32/9_2.png) [@exyi](https://forum.dotvvm.com/u/exyi)\
**Post date:** [August 12, 2024, 10:57am UTC](https://forum.dotvvm.com/t/problem-with-authentication/229/6 "2024-08-12T10:57:27Z")

</div>

> [@Dosmac](#):
>
> What other steps should I take to state that the /login url does not need to be authenticated so to resolve this problem?

You have to avoid calling Authorize in the specific case. For instance, using an `if (Context.Route.Name != "YourLoginPage")` condition or placing the Authorize call into a virtual method which you override to be empty in the LoginViewModel
