Getting current user id (not name) using forms authentication?

Question:

I play with forms authentication and my own custom membership provider.

From what I see, I can get the current FormsIdentity form by doing:

System.Web.Security.FormsIdentity veryFunny= (System.Web.Security.FormsIdentity)
    System.Web.HttpContext.Current.User.Identity;

And I can get the current membership user to do this:

        var UserFromDb = System.Web.Security.Membership.GetUser();

And then I can get userid by doing the following:

var MyUserId = UserFromDb.ProviderUserKey;

I find it funny that when I call Memberhip.GetUser (), it calls this method in the membership provider

public override MembershipUser GetUser(string username, bool userIsOnline)

What is looking for user information in the database.
So, I realized that the .NET framework is inside

GetUser(System.Web.HttpContext.Current.User.Identity.Name, whatever);

USERNAME. , -, , , .

-, , , , .

-, , , , , .

.
, microsoft , , .

, :
?

?

:
, FormsIdentity string, userdata. , ASP.NET ?

+5
1

, .

UserId UserData cookie Auth.

public class UserData
{
    public string FirstName { get; set; }
    public UserData()
    {
        FirstName = "Unknown";
    }
} // End Class UserData



public void SignIn(string userName, bool createPersistentCookie)
{
    if (String.IsNullOrEmpty(userName)) 
        throw new ArgumentException("Der Wert darf nicht NULL oder leer sein.", "userName");

    UserData userData = new UserData();
    SetAuthCookie(userName, createPersistentCookie, userData);
    //FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
}


public void SetAuthCookie(string userName, bool createPersistentCookie, UserData userData)
{
    HttpCookie cookie = FormsAuthentication.GetAuthCookie(userName, createPersistentCookie);
    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
    FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(
         ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration
        ,ticket.IsPersistent, userData.ToJSON(), ticket.CookiePath
    );

    string encTicket = FormsAuthentication.Encrypt(newTicket);
    cookie.Value = encTicket;
    System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
} // End Sub SetAuthCookie

- UserId.ToString() "".

+2

All Articles