Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The name 'DefaultAuthenticationTypes' does not exist in the current context

I'm trying to implement role-based authorization in my web application like following:

[HttpPost]
[ActionName("Login")]
public ActionResult Login(LoginViewModel model)
{
    if (ModelState.IsValid)
    {
        string userName = model.Username;
        string[] userRoles = (string[])Session["UserRoles"];

        ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);

        identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userName));

        userRoles.ToList().ForEach((role) => identity.AddClaim(new Claim(ClaimTypes.Role, role)));

        identity.AddClaim(new Claim(ClaimTypes.Name, userName));

        AuthenticationManager.SignIn(identity);

        return RedirectToAction("Success");
    }
    else
    {
        return View("Login",model);
    }
}

I'm getting an error on two lines:

1.ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);

And:

2.AuthenticationManager.SignIn(identity);

The error 1:

the name 'DefaultAuthenticationTypes' does not exist in the current context

And error 2 is:

Authentication manager does not contains definition for SignIn

I was trying to find a solution how to implement this but I couldn't find anything related to the errors.

like image 600
User987 Avatar asked Oct 28 '16 11:10

User987


People also ask

What is defaultauthenticationtypes in ASP NET?

DefaultAuthenticationTypes is part of Identity framework and found in Microsoft.AspNet.Identity namespace. To use it, add a using to the top of the file.

What is the default value of externalsigninauthenticationtype?

Default value used for the ExternalSignInAuthenticationType configured by UseSignInCookies. Default value for authentication type used for two factor remember browser. Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Does the project name XXX exist in the current context?

CS0103 The name XXX does not exist in the current context nn_ProjectName_FileName Note the project name appears to be some generated name consisting of a number, the actual project name and the file name (so for example 39_Commerce_PaymentEditor).


1 Answers

DefaultAuthenticationTypes is part of Identity framework and found in Microsoft.AspNet.Identity namespace.

To use it, add a using to the top of the file

using Microsoft.AspNet.Identity;
//...other code
identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);

or call it directly

identity = new ClaimsIdentity(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);

The second issue was already dealt with in another one of your questions here

like image 109
Nkosi Avatar answered Sep 28 '22 15:09

Nkosi