Razor Components AuthenticationThe definitive guide to form-based website authenticationRESTful AuthenticationHow should I choose an authentication library for CodeIgniter?What is token based authentication?How do I import a namespace in Razor View Page?Use basic authentication with jQuery and AjaxAuthentication versus AuthorizationAuthenticating a user in a MVC application using JSON Web TokensHow to use AuthenticationManager SignIn when using OpenIdCan authentication cookie be shared between two .Net Core 2.0 applications?

Why not 1.d4 Nf6 2.d5?

Can US Congress members be (successfully) sued for defamation?

だけ between two verbs / second verb performing an action on だけ construction

Is this really played by 2200+ players?

Is this sentence from a widely distributed current affairs publication correct?

Can every type of linear filter be modelled by a convolution?

How do social media apps notify you when someone else takes a screenshot of your profile?

Teaching asymptotic notations at the beginning of Calculus

Do any languages mark social distinctions other than gender and status?

Are ups & downs/peaks and valleys inherent in piano practice and performance?

Probability of a 500 year flood occuring in the next 100 years - comparison of approaches

Compress .hex file for micro-controller

Why apt asking to uninstall GIMP when installing ardour?

Why are there never-ending wars in the Middle East?

What does "away to insignificance" mean?

is it biologically possible for a creature that can be compatible to reproduce with any creature?

10x10 grid with no unpainted hexominoes

What does 'Rocket is in self align' mean?

Options for passes to national parks in Arizona/Utah for 5 people travelling in one car

When and why did the House rules change to permit an inquiry without a vote?

Diagnosing instant circuit breaker tripping, with nothing connected

Do I need a visa for Japan as a New Zealand Citizen?

Does using an img title attribute in addition to the alt attribute help image SEO?

Replacing each letter with the letter that is in the corresponding position from the end of the English alphabet



Razor Components Authentication


The definitive guide to form-based website authenticationRESTful AuthenticationHow should I choose an authentication library for CodeIgniter?What is token based authentication?How do I import a namespace in Razor View Page?Use basic authentication with jQuery and AjaxAuthentication versus AuthorizationAuthenticating a user in a MVC application using JSON Web TokensHow to use AuthenticationManager SignIn when using OpenIdCan authentication cookie be shared between two .Net Core 2.0 applications?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









0

















How would one use authentication SignInAsync as a call from a razor component.
It seems to throw some errors due to the context being in a late state of the pipeline.



Error when HttpContext.SignInAsync is called:



System.AggregateException: 'One or more errors occurred. (The response headers cannot be modified because the response has already started.)' 
InvalidOperationException: The response headers cannot be modified because the response has already started.


Here is the UserState.cs where the SignIn is implemented.



public class UserState


readonly IHttpContextAccessor Context;

public UserState(IHttpContextAccessor Context)

this.Context = Context;


public string DisplayName get; set;

public ClaimsPrincipal User => Context.HttpContext.User;

public bool IsLoggedIn => User?.Identity.IsAuthenticated ?? false;

public void SignIn()

var claims = new List<Claim>
new Claim(ClaimTypes.Name, "Bob", ClaimValueTypes.String),
new Claim(ClaimTypes.Surname, "Builder", ClaimValueTypes.String),
;
var userIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var userPrincipal = new ClaimsPrincipal(userIdentity);

Context.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, userPrincipal,
new AuthenticationProperties

ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
IsPersistent = true,
AllowRefresh = true
).Wait();




method is called and injected as such:



@page "/"
@inject UserState User

@if (User.IsLoggedIn)

<p>You are logged in</p>
else
<p>You are NOT logged in</p>


<button onclick="@User.SignIn">Login</button>


Is there a way to signin get a success response and then refresh the page from the SignIn method.



Ive seen some similar implementation using MVC controllers. Is there no way of invoking a authentication request without involving mvc?










share|improve this question


























  • <button onclick="@User.SignIn">Login</button> should go inside the else right?

    – dcg
    Mar 28 at 21:38











  • What does it mean <button onclick="@User.SignIn">Login</button>? It doesn't even compile

    – Alexander
    Mar 28 at 23:57











  • Using Razor Components .NET Core 3.0 (Visual Studio 2019)

    – Theodor Solbjørg
    Mar 29 at 0:02











  • Did you ever solve this? I have the same problem (Server-side Blazor).

    – Fred
    Jun 20 at 5:09











  • No, I ended up having a Razor Page that the app redirects to when auth is needed. seems messing with the httpcontext mid request is a big no-no, but weird that blazor does not have a pre-request handler we can hook in too. leaving it open for some genius to answer tho'

    – Theodor Solbjørg
    Jun 21 at 12:56

















0

















How would one use authentication SignInAsync as a call from a razor component.
It seems to throw some errors due to the context being in a late state of the pipeline.



Error when HttpContext.SignInAsync is called:



System.AggregateException: 'One or more errors occurred. (The response headers cannot be modified because the response has already started.)' 
InvalidOperationException: The response headers cannot be modified because the response has already started.


Here is the UserState.cs where the SignIn is implemented.



public class UserState


readonly IHttpContextAccessor Context;

public UserState(IHttpContextAccessor Context)

this.Context = Context;


public string DisplayName get; set;

public ClaimsPrincipal User => Context.HttpContext.User;

public bool IsLoggedIn => User?.Identity.IsAuthenticated ?? false;

public void SignIn()

var claims = new List<Claim>
new Claim(ClaimTypes.Name, "Bob", ClaimValueTypes.String),
new Claim(ClaimTypes.Surname, "Builder", ClaimValueTypes.String),
;
var userIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var userPrincipal = new ClaimsPrincipal(userIdentity);

Context.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, userPrincipal,
new AuthenticationProperties

ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
IsPersistent = true,
AllowRefresh = true
).Wait();




method is called and injected as such:



@page "/"
@inject UserState User

@if (User.IsLoggedIn)

<p>You are logged in</p>
else
<p>You are NOT logged in</p>


<button onclick="@User.SignIn">Login</button>


Is there a way to signin get a success response and then refresh the page from the SignIn method.



Ive seen some similar implementation using MVC controllers. Is there no way of invoking a authentication request without involving mvc?










share|improve this question


























  • <button onclick="@User.SignIn">Login</button> should go inside the else right?

    – dcg
    Mar 28 at 21:38











  • What does it mean <button onclick="@User.SignIn">Login</button>? It doesn't even compile

    – Alexander
    Mar 28 at 23:57











  • Using Razor Components .NET Core 3.0 (Visual Studio 2019)

    – Theodor Solbjørg
    Mar 29 at 0:02











  • Did you ever solve this? I have the same problem (Server-side Blazor).

    – Fred
    Jun 20 at 5:09











  • No, I ended up having a Razor Page that the app redirects to when auth is needed. seems messing with the httpcontext mid request is a big no-no, but weird that blazor does not have a pre-request handler we can hook in too. leaving it open for some genius to answer tho'

    – Theodor Solbjørg
    Jun 21 at 12:56













0












0








0


1






How would one use authentication SignInAsync as a call from a razor component.
It seems to throw some errors due to the context being in a late state of the pipeline.



Error when HttpContext.SignInAsync is called:



System.AggregateException: 'One or more errors occurred. (The response headers cannot be modified because the response has already started.)' 
InvalidOperationException: The response headers cannot be modified because the response has already started.


Here is the UserState.cs where the SignIn is implemented.



public class UserState


readonly IHttpContextAccessor Context;

public UserState(IHttpContextAccessor Context)

this.Context = Context;


public string DisplayName get; set;

public ClaimsPrincipal User => Context.HttpContext.User;

public bool IsLoggedIn => User?.Identity.IsAuthenticated ?? false;

public void SignIn()

var claims = new List<Claim>
new Claim(ClaimTypes.Name, "Bob", ClaimValueTypes.String),
new Claim(ClaimTypes.Surname, "Builder", ClaimValueTypes.String),
;
var userIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var userPrincipal = new ClaimsPrincipal(userIdentity);

Context.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, userPrincipal,
new AuthenticationProperties

ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
IsPersistent = true,
AllowRefresh = true
).Wait();




method is called and injected as such:



@page "/"
@inject UserState User

@if (User.IsLoggedIn)

<p>You are logged in</p>
else
<p>You are NOT logged in</p>


<button onclick="@User.SignIn">Login</button>


Is there a way to signin get a success response and then refresh the page from the SignIn method.



Ive seen some similar implementation using MVC controllers. Is there no way of invoking a authentication request without involving mvc?










share|improve this question















How would one use authentication SignInAsync as a call from a razor component.
It seems to throw some errors due to the context being in a late state of the pipeline.



Error when HttpContext.SignInAsync is called:



System.AggregateException: 'One or more errors occurred. (The response headers cannot be modified because the response has already started.)' 
InvalidOperationException: The response headers cannot be modified because the response has already started.


Here is the UserState.cs where the SignIn is implemented.



public class UserState


readonly IHttpContextAccessor Context;

public UserState(IHttpContextAccessor Context)

this.Context = Context;


public string DisplayName get; set;

public ClaimsPrincipal User => Context.HttpContext.User;

public bool IsLoggedIn => User?.Identity.IsAuthenticated ?? false;

public void SignIn()

var claims = new List<Claim>
new Claim(ClaimTypes.Name, "Bob", ClaimValueTypes.String),
new Claim(ClaimTypes.Surname, "Builder", ClaimValueTypes.String),
;
var userIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var userPrincipal = new ClaimsPrincipal(userIdentity);

Context.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, userPrincipal,
new AuthenticationProperties

ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
IsPersistent = true,
AllowRefresh = true
).Wait();




method is called and injected as such:



@page "/"
@inject UserState User

@if (User.IsLoggedIn)

<p>You are logged in</p>
else
<p>You are NOT logged in</p>


<button onclick="@User.SignIn">Login</button>


Is there a way to signin get a success response and then refresh the page from the SignIn method.



Ive seen some similar implementation using MVC controllers. Is there no way of invoking a authentication request without involving mvc?







c# asp.net authentication razor-components






share|improve this question














share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 21:31









Theodor SolbjørgTheodor Solbjørg

5735 silver badges15 bronze badges




5735 silver badges15 bronze badges















  • <button onclick="@User.SignIn">Login</button> should go inside the else right?

    – dcg
    Mar 28 at 21:38











  • What does it mean <button onclick="@User.SignIn">Login</button>? It doesn't even compile

    – Alexander
    Mar 28 at 23:57











  • Using Razor Components .NET Core 3.0 (Visual Studio 2019)

    – Theodor Solbjørg
    Mar 29 at 0:02











  • Did you ever solve this? I have the same problem (Server-side Blazor).

    – Fred
    Jun 20 at 5:09











  • No, I ended up having a Razor Page that the app redirects to when auth is needed. seems messing with the httpcontext mid request is a big no-no, but weird that blazor does not have a pre-request handler we can hook in too. leaving it open for some genius to answer tho'

    – Theodor Solbjørg
    Jun 21 at 12:56

















  • <button onclick="@User.SignIn">Login</button> should go inside the else right?

    – dcg
    Mar 28 at 21:38











  • What does it mean <button onclick="@User.SignIn">Login</button>? It doesn't even compile

    – Alexander
    Mar 28 at 23:57











  • Using Razor Components .NET Core 3.0 (Visual Studio 2019)

    – Theodor Solbjørg
    Mar 29 at 0:02











  • Did you ever solve this? I have the same problem (Server-side Blazor).

    – Fred
    Jun 20 at 5:09











  • No, I ended up having a Razor Page that the app redirects to when auth is needed. seems messing with the httpcontext mid request is a big no-no, but weird that blazor does not have a pre-request handler we can hook in too. leaving it open for some genius to answer tho'

    – Theodor Solbjørg
    Jun 21 at 12:56
















<button onclick="@User.SignIn">Login</button> should go inside the else right?

– dcg
Mar 28 at 21:38





<button onclick="@User.SignIn">Login</button> should go inside the else right?

– dcg
Mar 28 at 21:38













What does it mean <button onclick="@User.SignIn">Login</button>? It doesn't even compile

– Alexander
Mar 28 at 23:57





What does it mean <button onclick="@User.SignIn">Login</button>? It doesn't even compile

– Alexander
Mar 28 at 23:57













Using Razor Components .NET Core 3.0 (Visual Studio 2019)

– Theodor Solbjørg
Mar 29 at 0:02





Using Razor Components .NET Core 3.0 (Visual Studio 2019)

– Theodor Solbjørg
Mar 29 at 0:02













Did you ever solve this? I have the same problem (Server-side Blazor).

– Fred
Jun 20 at 5:09





Did you ever solve this? I have the same problem (Server-side Blazor).

– Fred
Jun 20 at 5:09













No, I ended up having a Razor Page that the app redirects to when auth is needed. seems messing with the httpcontext mid request is a big no-no, but weird that blazor does not have a pre-request handler we can hook in too. leaving it open for some genius to answer tho'

– Theodor Solbjørg
Jun 21 at 12:56





No, I ended up having a Razor Page that the app redirects to when auth is needed. seems messing with the httpcontext mid request is a big no-no, but weird that blazor does not have a pre-request handler we can hook in too. leaving it open for some genius to answer tho'

– Theodor Solbjørg
Jun 21 at 12:56












0






active

oldest

votes













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55407159%2frazor-components-authentication%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown


























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55407159%2frazor-components-authentication%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown









Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript