Click here to Skip to main content
14,867,123 members
Articles / Programming Languages / C#
Tip/Trick
Posted 29 Jul 2016

Tagged as

Stats

23.1K views
4 bookmarked

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
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)

Share

About the Author

Pawel Sienko
Poland Poland
No Biography provided

Comments and Discussions

 
QuestionNot quite true Pin
yxhu31-Jul-16 20:23
Memberyxhu31-Jul-16 20:23 
AnswerRe: Not quite true Pin
Pawel Sienko1-Aug-16 0:22
MemberPawel Sienko1-Aug-16 0:22 
GeneralRe: Not quite true Pin
yxhu1-Aug-16 15:35
Memberyxhu1-Aug-16 15:35 
GeneralRe: Not quite true Pin
Pawel Sienko1-Aug-16 23:29
MemberPawel Sienko1-Aug-16 23:29 
AnswerRe: Not quite true Pin
Ed Bouras1-Aug-16 7:02
MemberEd Bouras1-Aug-16 7:02 
GeneralRe: Not quite true Pin
yxhu1-Aug-16 15:23
Memberyxhu1-Aug-16 15:23 
GeneralRe: Not quite true Pin
Ed Bouras2-Aug-16 2:25
MemberEd Bouras2-Aug-16 2:25 
GeneralRe: Not quite true Pin
yxhu2-Aug-16 2:39
Memberyxhu2-Aug-16 2:39 
GeneralRe: Not quite true Pin
Ed Bouras2-Aug-16 2:43
MemberEd Bouras2-Aug-16 2:43 
GeneralRe: Not quite true Pin
yxhu2-Aug-16 3:04
Memberyxhu2-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.