Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Forbidden page

$
0
0

Hi guys, I've tried to use Cookie Authentication, so:

Configure Method:

app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
	AuthenticationScheme = "UwpCookieMiddlewareInstance",
	LoginPath = new PathString("/Account/Unauthorized/"),
	AccessDeniedPath = new PathString("/Account/Forbidden/"),
	AutomaticAuthenticate = true,
	AutomaticChallenge = true,
})
app.Run(async context =>
{
	var user = context.User;

	// Deny anonymous request beyond this point.
	if (user == null || !user.Identities.Any(identity => identity.IsAuthenticated))
	{
		await context.Authentication.ChallengeAsync();

		return;
	}
});

ConfigureServices method:

services.AddAuthentication(options => options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);

SignIn action:

const string uwpCookieMiddlewareInstance = "UwpCookieMiddlewareInstance";

public async Task<IActionResult> SignIn()
{
	var claimsPrincipal = new ClaimsPrincipal();

	var claimsIdentity = new ClaimsIdentity();

	var claim = new Claim("login", "urban.michal@icloud.com");

	claimsIdentity.AddClaim(claim);

	claimsPrincipal.AddIdentity(claimsIdentity);

	await HttpContext.Authentication.SignInAsync(uwpCookieMiddlewareInstance, claimsPrincipal);

	return RedirectToAction("Index", "Home");
}		

and when I'm executing this action and when [Authorize] attribute is added to Home controller it's get redirected to Forbidden page. When I get rid of attribute, access is granted. When I set attribute in that way:

[Authorize(CookieAuthenticationDefaults.AuthenticationScheme)

Server returns such exception:

System.InvalidOperationException: The AuthorizationPolicy named: 'Cookies' was not found.
   at Microsoft.AspNetCore.Authorization.AuthorizationPolicy.Combine(AuthorizationOptions options, IEnumerable`1 attributes)
   at Microsoft.AspNetCore.Mvc.Internal.AuthorizationApplicationModelProvider.OnProvidersExecuting(ApplicationModelProviderContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionDescriptorProvider.BuildModel()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionDescriptorProvider.GetDescriptors()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ActionDescriptorCollectionProvider.GetCollection()
   at Microsoft.AspNetCore.Mvc.Internal.ActionDescriptorCollectionProvider.get_ActionDescriptors()
   at Microsoft.AspNetCore.Mvc.Internal.AttributeRoute.GetTreeRouter()
   at Microsoft.AspNetCore.Mvc.Internal.AttributeRoute.RouteAsync(RouteContext context)
   at Microsoft.AspNetCore.Routing.RouteCollection.<RouteAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Web.BrowserLink.Runtime.BrowserLinkMiddleware.<ExecuteWithFilter>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext()

Any ideas what I'm doing wrong or what is omitted?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>