-1

I want to enable windows authentication for my ASP.NET intranet application. For that, I did the below in my web.config file.

<system.web>
 <authentication mode="Windows" >

    </authentication>
    <authorization>
      <deny users="?"/>
    </authorization>
    <identity impersonate="true" userName="domain\myusername" password="mypwd" />
</system.web>

And I am trying to access the authenticated user name in my web page as

HttpContext.Current.User.Identity.Name

It throws an "access denied" error message.

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
Karthikeyan
  • 299
  • 1
  • 10
  • 25
  • So do you have to get the Impersonated name there in your web page ? – Dhrumil Apr 28 '15 at 10:57
  • no, am not getting any user name here, It throws me access denied error. When I removed the authorization tag, I could access my page, but not getting the impersonated user . – Karthikeyan Apr 28 '15 at 11:01
  • Try using `WindowsIdentity.Name` or `WindowsIdentity.GetCurrent().Name` instead of `HttpContext.Current.User.Identity.Name`. – Dhrumil Apr 28 '15 at 11:03
  • hi, WindowsIdentity.GetCurrent().Name works correctly. Thanks for the info. and can I add some roles in UserClaims from SQL table?? – Karthikeyan Apr 28 '15 at 11:21
  • Probably yes. Check my answer. – Dhrumil Apr 28 '15 at 11:26
  • If you are after authenticating against active directory on your intranet, then impersonating isn't for you. – user3036342 Apr 28 '15 at 11:29
  • One more question is, why am not getting the logged user by using HttpContext.Current.User.Identity.Name Is there any specific reason?? – Karthikeyan Apr 28 '15 at 11:35
  • Not sure as to why that is not working.I am checking that too. You can try setting the name in cookie and then retrieving it for further use. – Dhrumil Apr 28 '15 at 11:44

1 Answers1

3

Try using WindowsIdentity.Name or WindowsIdentity.GetCurrent().Name instead of HttpContext.Current.User.Identity.Name.

As far as adding roles to Claim is concerned, you can add it with the help of some code. These links might help you :

Creating ClaimIdentity Object

Claim Based Security in C#

Hope this helps.

Community
  • 1
  • 1
Dhrumil
  • 3,221
  • 6
  • 21
  • 34