Click here to Skip to main content
15,887,135 members
Articles / Programming Languages / C#
Tip/Trick

Request.IsAuthenticated is Always True After Call FormsAuthentication.Signout()

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
29 Jul 2016CPOL 35.7K   4   10
This trick describes how to fix issue.

Introduction

When you work with Forms Authentication, the expected behaviour when you call FormsAuthentication.Signout() is that Http.Current.Request.IsAuthenticated will return false.

You are wrong.

Using the Code

C#
FormsAuthentication.SignOut();

When you check this:

C#
bool isAuthenticated = Request.IsAuthenticated;

the result is always true.

It's not what we expect when signout is performed.

Fix the Issue

To fix it after signout process, you need to assign new user like below:

C#
FormsAuthentication.SignOut();
HttpContext.Current.User =
    new GenericPrincipal(new GenericIdentity(string.Empty), null);

The new GenericPrincipal with GenericIdentity is assigned to user in current context. New identity with empty name and null as authentication type.

Then it works!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNot quite true Pin
yxhu31-Jul-16 20:23
yxhu31-Jul-16 20:23 
AnswerRe: Not quite true Pin
Pawel Sienko1-Aug-16 0:22
Pawel Sienko1-Aug-16 0:22 
GeneralRe: Not quite true Pin
yxhu1-Aug-16 15:35
yxhu1-Aug-16 15:35 
GeneralRe: Not quite true Pin
Pawel Sienko1-Aug-16 23:29
Pawel Sienko1-Aug-16 23:29 
AnswerRe: Not quite true Pin
Middle Manager1-Aug-16 7:02
Middle Manager1-Aug-16 7:02 
GeneralRe: Not quite true Pin
yxhu1-Aug-16 15:23
yxhu1-Aug-16 15:23 
GeneralRe: Not quite true Pin
Middle Manager2-Aug-16 2:25
Middle Manager2-Aug-16 2:25 
GeneralRe: Not quite true Pin
yxhu2-Aug-16 2:39
yxhu2-Aug-16 2:39 
GeneralRe: Not quite true Pin
Middle Manager2-Aug-16 2:43
Middle Manager2-Aug-16 2:43 
GeneralRe: Not quite true Pin
yxhu2-Aug-16 3:04
yxhu2-Aug-16 3:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.