0

I am trying to get the user Id using C# Code as below:

string username = System.Web.HttpContext.Current.User.Identity.Name;

I am getting the username and credetial like this

username:"ACSIND\11111111"

here userid is is '11111111'

but I am not able to get the userid alone from the code.

I have done this in VB as like below:

   Option Compare Database
   Option Explicit
   Private Declare Function GetUserName Lib "ADVAPI32.dll" Alias "GetUserNameA" _
   (ByVal lpBuffer As String, nSize As Long) As Long

    Public Function GetCurrentUserName() As String
   On Error GoTo Err_GetCurrentUserName
    Dim lpBuff As String * 25
    Dim ret As Long, Username As String
   ret = GetUserName(lpBuff, 25)
   Username = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
  GetCurrentUserName = Username & ""

   Exit_GetCurrentUserName:
   Exit Function

    Err_GetCurrentUserName:
    MsgBox Err.Description
    Resume Exit_GetCurrentUserName
    End Function

Please help how to get userId alone in C#

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
FaisalThayyil
  • 85
  • 1
  • 3
  • 14
  • 4
    http://stackoverflow.com/a/350142/2258 – Richard Morgan Nov 11 '13 at 17:42
  • @RichardMorgan The answer to that question includes parsing and is applicable only to web as it uses `HttpContext`. The user did not mark this as a web question. – P.Brian.Mackey Nov 11 '13 at 17:50
  • @P.Brian.Mackey Where does `System.Web.HttpContext.Current` (from the second line of the question) exist outside of web (e.g. ASP.Net)? –  Nov 11 '13 at 17:54
  • My mistake, I did not notice the `HttpContext` in the question. Its missing from the Tags as well. – P.Brian.Mackey Nov 11 '13 at 17:56
  • @P.Brian.Mackey Well, now I'm not sure he may be just using that as an example of what he's used to. (and of course the code-sample is VB, maybe 6?) (note: my comment was about your comment, not your answer the answer was good!) –  Nov 11 '13 at 17:57
  • `Environment.UserName` might work on the web side. Assuming the domain username matches whatever the users authenticate with on the web. – P.Brian.Mackey Nov 11 '13 at 18:05
  • I am trying to get the user id and login time for capturing attendance in start up – FaisalThayyil Nov 11 '13 at 18:11
  • What do you mean by startup (global.asax)? What kind of application is this (asp.net?) – P.Brian.Mackey Nov 11 '13 at 18:12

0 Answers0