Thierry Verhaegen
Thierry Verhaegen

Reputation: 195

NET Identity in web.config

We have an old website in (.NET Framework, Web application) here that was created years ago and where we still do ongoing development. Because we do not have enough time to rewrite everything, I want to create a new website next to this one, and gradually move functionality over. However I want to re-use some if not all class libraries. Most of these libraries cannot be re-directed to .net standard because it has references to our user object. This user object implements MembershipUser... which is of course no longer available in Core.

So they seem to have implemented MembershipUser, but they did almost everything custom, most of the functions in membershipprovider have not been implemented, only some.

I want to invest as little as time possible in this old website, so here is what i tried:

I installed the packages Microsoft.AspNet.Identity.Core (and Owin where necessary, i choose these because it has to work for .NET Framework as well as Core). Removed the derivation from MembershipUser on the user object, and instead have it derive from IdentityUser. IdentityUser is a custom class I inherited from IUser.

I removed our Custom membershipProvider class which derived from MembershipProvider (which only implemented a few functions). And I made a class that derived from UserStore<> and also UserPasswordStore. So far so good (i think).

Where i'm struggling now is this piece of code:

private void SetAuthTicket(HttpContext httpContext, string username)
{
    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
        1, username, DateTime.Now, DateTime.Now.AddMinutes(SessionDuration), true, FormsAuthentication.FormsCookiePath);
    string ticketHash = FormsAuthentication.Encrypt(ticket);
    HttpCookie ticketCookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketHash);
    ticketCookie.Expires = DateTime.Now.AddMinutes(SessionDuration); //--> Removed Double duration possible loginbug cause//Double session duration because sliding only updates after half the exp time has been exceeded 
    httpContext.Response.Cookies.Add(ticketCookie);
    int uid = ((User)HttpContext.Current.User).Id;

    string hostName = Dns.GetHostName(); // Retrieve the Name of HOST  
    var IPAddress = Dns.GetHostEntry(hostName).AddressList[Dns.GetHostEntry(hostName).AddressList.Length-1].ToString();
    var externalIp = getExternalIP();
    var UserAgent = HttpContext.Current.Request.UserAgent;

    LoginTicket dbticket = new LoginTicket()
    {
        CreatedBy = new EntityReference<User>(uid),
        Ticket = ticketHash,
        UserId = uid,
        UserAgent = UserAgent,
        IPaddress = IPAddress,
        ExternalIPaddress = externalIp?.ToString()
    };
    DBA.Save(dbticket);

}

Since FormsAuthenticationicket and FormsAuthentication is gone in Core/Standard, how do i go about rewriting this? I want to keep the cookie authentication in the old website. But when googling I only find the HttpContext.SignInAsync() function, which is in the Microsoft.AspNetCore.Identity assemblies. Which I didn't use cause i needed compatibility with .NET Framework. And all courses/video's I find are based on MVC functionality, while we are using a web application and thus a web.config. Can anyone point me in the direction of a good tutorial, or has an explanation on how to go about this?

Upvotes: 0

Views: 38

Answers (0)

Related Questions