你好朋友我想问你,任何人都可以举例将oAuth集成到ASP.Net web api中,无需集成任何包或实体框架 ??我搜索它很多,但找到使用nuget包和其他包的各种方式,但我需要使用简单的第三方调用的方式,因为我需要在.net以及java api的这个授权。任何人都可以帮助我解决这个问题。使用ASP.NET WebAPI实现OAuth
在此先感谢...
你好朋友我想问你,任何人都可以举例将oAuth集成到ASP.Net web api中,无需集成任何包或实体框架 ??我搜索它很多,但找到使用nuget包和其他包的各种方式,但我需要使用简单的第三方调用的方式,因为我需要在.net以及java api的这个授权。任何人都可以帮助我解决这个问题。使用ASP.NET WebAPI实现OAuth
在此先感谢...
是的,你可以做到这一点,我在网上API 2项目使用OAuth实现了这个在我的web API。
首先,有一个asp.net项目与oauth配置,因为我们将cooy一些文件放入web api项目。
以下是步骤: 1)在web api中,添加一个名为“IdentityConfig.cs”的新类文件。
该类将具有:ApplicationUser,ApplicationUserManager,ApplicationSignInManager和ApplicationDbContext类。
2)确保上面的这些类位于您的API名称空间下方,以便通过您的所有控制器访问它们。
// Configure the application user manager which is used in this api.
public class ApplicationUser : IdentityUser
{
#region custom properties
public string Name { get; set; }
public int? ZipCode { get; set; }
public long? CountryId { get; set; }
public bool IsDeleted { get; set; }
public bool EmailConfirmed { get; set; }
public DateTime CreatedDate { get; set; }
public long UserId { get; set; }
#endregion
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = true,
RequireUppercase = false,
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
// You can write your own provider and plug it in here.
manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
{
MessageFormat = "Your security code is {0}"
});
manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
{
Subject = "Security Code",
BodyFormat = "Your security code is {0}"
});
manager.EmailService = new EmailService();
//manager.SmsService = new SmsService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
// Configure the application sign-in manager which is used in this api.
public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
: base(userManager, authenticationManager)
{
}
public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
{
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
}
public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
{
return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DBCONNECTIONKEY", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
注:DBCONNECTIONKEY是web.config中的连接字符串
3)Startup.cs文件添加到您的Web API的根密钥。复制你现有的在asp.net中的逻辑。随时根据需要调整web api项目中的配置上下文属性。
4)使用这些类中的对象登录用户,并管理应用程序用户对象,就像在asp.net Web应用程序中一样。
这就是所有:)
希望这有助于。