你好朋友我想問你,任何人都可以舉例將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應用程序中一樣。
這就是所有:)
希望這有助於。