0

I am trying to login user through webapi. My apicontroller function is:

public async Task<IHttpActionResult> Login(string email, string password)
        {
            ApplicationDbContext ctx = new ApplicationDbContext();
            UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(ctx);
            UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(store);
            var user = await UserManager.FindAsync(email, password);
            if (user != null)
            {
                await SignInAsync(user, true); // The name 'SignInAsync' does not exist in current context
                return Ok("OK");
            }
            return Ok("Error");
        }

I want to write methods of signup, login, and logout in webapi but i am stuck at SignInAsync. Am I missing library reference? Or how to use this in webapi?

Irfan Y
  • 1,242
  • 2
  • 21
  • 68

1 Answers1

1

SignInAsync is a method of SignInManager class not controller class write this instead:

await HttpContext.Current.GetOwinContext()
    .Get<ApplicationSignInManager>().SignInAsync(user, true, false); 
Sam FarajpourGhamari
  • 14,601
  • 4
  • 52
  • 56
  • `GetOwinContext` is not coming with `HttpContext.`. I have included namepace `Microsoft.AspNet.Identity.Owin` – Irfan Y Sep 20 '15 at 06:18
  • You must install Microsoft. Owin.host.systemweb packege – Sam FarajpourGhamari Sep 20 '15 at 09:15
  • Ok I have installed package using this command `PM> Install-Package Microsoft.Owin.Host.SystemWeb` but still `HttpContext.` is not showing `GetOwinContext` in IntelliSense. – Irfan Y Sep 20 '15 at 09:33
  • Did you configured Owin? – Sam FarajpourGhamari Sep 20 '15 at 14:39
  • How to configure Owin? – Irfan Y Sep 20 '15 at 15:13
  • Please read my earlier answer about [how to configuring Identity](http://stackoverflow.com/questions/31960433/adding-asp-net-mvc5-identity-authentication-to-an-existing-project/31963828#31963828). It is about MVC but you could do same in WebAPI too. – Sam FarajpourGhamari Sep 20 '15 at 15:18
  • Thank you for the link. I think the portion useful for me from answer is from "Add a class to App_Start folder" but a class is already added to app_Start folder named `Startup.Auth.cs`. On `ConfigureAuth` method of this class I am stuck on second line which is: `app.CreatePerOwinContext(AppUserManager.Create);` I don't know what to write instead of `AppUserManager` I have tried `UserManager` and `ApplicationUser` but none of them is working. – Irfan Y Sep 20 '15 at 17:06
  • Could you please submit your `Startup.Auth` content? – Sam FarajpourGhamari Sep 20 '15 at 17:27
  • sorry name of that file is `Startup.Auth.cs` and the class inside it is a partial class named `Startup` – Irfan Y Sep 20 '15 at 17:41
  • this class has a method defined as: `public void ConfigureAuth(IAppBuilder app) {app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login") });app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);` – Irfan Y Sep 20 '15 at 17:44
  • Why you are stuck? If you follow my instruction in the link. I mentioned how to create those classes. Are you using Identity's default template? Or installed after project creation? – Sam FarajpourGhamari Sep 20 '15 at 19:11
  • I am using Identity default template. I don't know what to write instead of `AppUserManager` in `app.CreatePerOwinContext(AppUserManager.Create);` – Irfan Y Sep 21 '15 at 04:36
  • If you are using default template. You must have `ApplicationUserManager` class in your project. So you could write `app.CreatePerOwinContext(ApplicationUserManager.Create)` – Sam FarajpourGhamari Sep 21 '15 at 19:12
  • No I don't have class of `ApplicationUserManager` instead I have `ApplicaitonUser` which is not working. I am using asp.net Identity. When I created project I checked `Individual User Authentication`. After that I didn't make any changes in store or anywhere related to identity. I am using data-first approach. – Irfan Y Sep 22 '15 at 04:37
  • So make new class which extends `UserManager` just like I did in the post. In my post `AppUser` is actually your `ApplicaitonUser`. Also you could omit `AppRole` related parts since you already had. – Sam FarajpourGhamari Sep 22 '15 at 08:49
  • Hi , In your solution I am stuck at `RoleStore` class. it says `the type or namespace RoleStore could not be found`. – Irfan Y Oct 05 '15 at 12:24
  • Add `Microsoft.AspNet.Identity.EntityFramework` namespace – Sam FarajpourGhamari Oct 05 '15 at 17:04
  • I have write the code as you said in answer but it says `System.web.httpContext does not contain definition for GetOwinContext` in webapi controller. – Irfan Y Oct 06 '15 at 03:22
  • Make sure you added `Microsoft.Owin` namespace. Also you already installed `Microsoft.Owin.Host.SystemWeb` also because you are using WebAPI write `HttpContext.Current.GetOwinContext() ...` instead. – Sam FarajpourGhamari Oct 06 '15 at 09:03